summaryrefslogtreecommitdiff
path: root/13
diff options
context:
space:
mode:
authorPrefetch2022-12-31 22:21:39 +0100
committerPrefetch2022-12-31 22:21:39 +0100
commit68615a9ad2c942254135cffb00cf25a84a3b1356 (patch)
tree1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /13
Initial commit
Diffstat (limited to '13')
-rw-r--r--13/input.txt56
-rwxr-xr-x13/main.py72
-rwxr-xr-x13/test.py30
3 files changed, 158 insertions, 0 deletions
diff --git a/13/input.txt b/13/input.txt
new file mode 100644
index 0000000..35be357
--- /dev/null
+++ b/13/input.txt
@@ -0,0 +1,56 @@
+Alice would gain 54 happiness units by sitting next to Bob.
+Alice would lose 81 happiness units by sitting next to Carol.
+Alice would lose 42 happiness units by sitting next to David.
+Alice would gain 89 happiness units by sitting next to Eric.
+Alice would lose 89 happiness units by sitting next to Frank.
+Alice would gain 97 happiness units by sitting next to George.
+Alice would lose 94 happiness units by sitting next to Mallory.
+Bob would gain 3 happiness units by sitting next to Alice.
+Bob would lose 70 happiness units by sitting next to Carol.
+Bob would lose 31 happiness units by sitting next to David.
+Bob would gain 72 happiness units by sitting next to Eric.
+Bob would lose 25 happiness units by sitting next to Frank.
+Bob would lose 95 happiness units by sitting next to George.
+Bob would gain 11 happiness units by sitting next to Mallory.
+Carol would lose 83 happiness units by sitting next to Alice.
+Carol would gain 8 happiness units by sitting next to Bob.
+Carol would gain 35 happiness units by sitting next to David.
+Carol would gain 10 happiness units by sitting next to Eric.
+Carol would gain 61 happiness units by sitting next to Frank.
+Carol would gain 10 happiness units by sitting next to George.
+Carol would gain 29 happiness units by sitting next to Mallory.
+David would gain 67 happiness units by sitting next to Alice.
+David would gain 25 happiness units by sitting next to Bob.
+David would gain 48 happiness units by sitting next to Carol.
+David would lose 65 happiness units by sitting next to Eric.
+David would gain 8 happiness units by sitting next to Frank.
+David would gain 84 happiness units by sitting next to George.
+David would gain 9 happiness units by sitting next to Mallory.
+Eric would lose 51 happiness units by sitting next to Alice.
+Eric would lose 39 happiness units by sitting next to Bob.
+Eric would gain 84 happiness units by sitting next to Carol.
+Eric would lose 98 happiness units by sitting next to David.
+Eric would lose 20 happiness units by sitting next to Frank.
+Eric would lose 6 happiness units by sitting next to George.
+Eric would gain 60 happiness units by sitting next to Mallory.
+Frank would gain 51 happiness units by sitting next to Alice.
+Frank would gain 79 happiness units by sitting next to Bob.
+Frank would gain 88 happiness units by sitting next to Carol.
+Frank would gain 33 happiness units by sitting next to David.
+Frank would gain 43 happiness units by sitting next to Eric.
+Frank would gain 77 happiness units by sitting next to George.
+Frank would lose 3 happiness units by sitting next to Mallory.
+George would lose 14 happiness units by sitting next to Alice.
+George would lose 12 happiness units by sitting next to Bob.
+George would lose 52 happiness units by sitting next to Carol.
+George would gain 14 happiness units by sitting next to David.
+George would lose 62 happiness units by sitting next to Eric.
+George would lose 18 happiness units by sitting next to Frank.
+George would lose 17 happiness units by sitting next to Mallory.
+Mallory would lose 36 happiness units by sitting next to Alice.
+Mallory would gain 76 happiness units by sitting next to Bob.
+Mallory would lose 34 happiness units by sitting next to Carol.
+Mallory would gain 37 happiness units by sitting next to David.
+Mallory would gain 40 happiness units by sitting next to Eric.
+Mallory would gain 18 happiness units by sitting next to Frank.
+Mallory would gain 7 happiness units by sitting next to George.
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()
diff --git a/13/test.py b/13/test.py
new file mode 100755
index 0000000..7600159
--- /dev/null
+++ b/13/test.py
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+
+import unittest
+
+import main
+
+
+
+class ExamplesPart1(unittest.TestCase):
+ def test_example1(self):
+ lines = [
+ "Alice would gain 54 happiness units by sitting next to Bob.",
+ "Alice would lose 79 happiness units by sitting next to Carol.",
+ "Alice would lose 2 happiness units by sitting next to David.",
+ "Bob would gain 83 happiness units by sitting next to Alice.",
+ "Bob would lose 7 happiness units by sitting next to Carol.",
+ "Bob would lose 63 happiness units by sitting next to David.",
+ "Carol would lose 62 happiness units by sitting next to Alice.",
+ "Carol would gain 60 happiness units by sitting next to Bob.",
+ "Carol would gain 55 happiness units by sitting next to David.",
+ "David would gain 46 happiness units by sitting next to Alice.",
+ "David would lose 7 happiness units by sitting next to Bob.",
+ "David would gain 41 happiness units by sitting next to Carol."
+ ]
+ self.assertEqual(main.solve_part1(lines), 330)
+
+
+
+if __name__ == "__main__":
+ unittest.main()