From 68615a9ad2c942254135cffb00cf25a84a3b1356 Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 31 Dec 2022 22:21:39 +0100 Subject: Initial commit --- 13/main.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 13/main.py (limited to '13/main.py') diff --git a/13/main.py b/13/main.py new file mode 100755 index 0000000..31bd337 --- /dev/null +++ b/13/main.py @@ -0,0 +1,72 @@ +#!/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() -- cgit v1.2.3