#!/usr/bin/python from itertools import permutations def parse_input(lines): data = {} for l in lines: words = l.split() name1 = words[ 0] name2 = words[10].rstrip(".") if words[2] == "gain": score = int(words[3]) else: # words[2] == "lose" score = -int(words[3]) if name1 not in data: data[name1] = {} data[name1][name2] = score return data def best_score(data): names = list(data.keys()) perms = permutations(names) scores = [] for p in perms: score = 0 for i in range(len(p)): n1 = p[i] n2 = p[(i + 1) % len(p)] score += data[n1][n2] + data[n2][n1] scores.append(score) return max(scores) def solve_part1(lines): data = parse_input(lines) return best_score(data) def solve_part2(lines): data = parse_input(lines) data["Myself"] = {} names = list(data.keys()) for n in names: data["Myself"][n] = 0 data[n]["Myself"] = 0 return best_score(data) def main(): # Read happiness relations from input text file with open("input.txt", "r") as f: lines = f.read().splitlines() print("Part 1 solution:", solve_part1(lines)) # 709 for me print("Part 2 solution:", solve_part2(lines)) # 668 for me if __name__ == "__main__": main()