summaryrefslogtreecommitdiff
path: root/13/main.py
diff options
context:
space:
mode:
authorPrefetch2022-12-31 22:21:39 +0100
committerPrefetch2022-12-31 22:21:39 +0100
commit68615a9ad2c942254135cffb00cf25a84a3b1356 (patch)
tree1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /13/main.py
Initial commit
Diffstat (limited to '13/main.py')
-rwxr-xr-x13/main.py72
1 files changed, 72 insertions, 0 deletions
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()