Browse Source

Day 13

master
Chris Smith 8 years ago
parent
commit
34fe1a69c6
2 changed files with 85 additions and 0 deletions
  1. 29
    0
      13.py
  2. 56
    0
      13.txt

+ 29
- 0
13.py View File

@@ -0,0 +1,29 @@
1
+#!/usr/bin/python
2
+
3
+from collections import defaultdict
4
+from itertools import permutations
5
+import re
6
+
7
+pattern = re.compile('^(.*) would (lose|gain) ([0-9]+) happiness .* next to (.*).')
8
+happiness = defaultdict(int)
9
+names = set()
10
+
11
+with open('13.txt', 'r') as file:
12
+    for line in file.readlines():
13
+        first, type, value, second = pattern.match(line).groups()
14
+        happiness[frozenset((first, second))] += (-1 if type == 'lose' else 1) * int(value)
15
+        names.add(first)
16
+
17
+
18
+def get_score(order):
19
+    return sum(happiness[frozenset(pair)] for pair in zip(order, order[1:] + [order[0]]))
20
+
21
+
22
+def find_best(people):
23
+    remaining = people.copy()
24
+    first = remaining.pop()
25
+    return max(get_score([first] + list(order)) for order in permutations(remaining))
26
+
27
+
28
+print(find_best(names))
29
+print(find_best(names.union("me")))

+ 56
- 0
13.txt View File

@@ -0,0 +1,56 @@
1
+Alice would gain 2 happiness units by sitting next to Bob.
2
+Alice would gain 26 happiness units by sitting next to Carol.
3
+Alice would lose 82 happiness units by sitting next to David.
4
+Alice would lose 75 happiness units by sitting next to Eric.
5
+Alice would gain 42 happiness units by sitting next to Frank.
6
+Alice would gain 38 happiness units by sitting next to George.
7
+Alice would gain 39 happiness units by sitting next to Mallory.
8
+Bob would gain 40 happiness units by sitting next to Alice.
9
+Bob would lose 61 happiness units by sitting next to Carol.
10
+Bob would lose 15 happiness units by sitting next to David.
11
+Bob would gain 63 happiness units by sitting next to Eric.
12
+Bob would gain 41 happiness units by sitting next to Frank.
13
+Bob would gain 30 happiness units by sitting next to George.
14
+Bob would gain 87 happiness units by sitting next to Mallory.
15
+Carol would lose 35 happiness units by sitting next to Alice.
16
+Carol would lose 99 happiness units by sitting next to Bob.
17
+Carol would lose 51 happiness units by sitting next to David.
18
+Carol would gain 95 happiness units by sitting next to Eric.
19
+Carol would gain 90 happiness units by sitting next to Frank.
20
+Carol would lose 16 happiness units by sitting next to George.
21
+Carol would gain 94 happiness units by sitting next to Mallory.
22
+David would gain 36 happiness units by sitting next to Alice.
23
+David would lose 18 happiness units by sitting next to Bob.
24
+David would lose 65 happiness units by sitting next to Carol.
25
+David would lose 18 happiness units by sitting next to Eric.
26
+David would lose 22 happiness units by sitting next to Frank.
27
+David would gain 2 happiness units by sitting next to George.
28
+David would gain 42 happiness units by sitting next to Mallory.
29
+Eric would lose 65 happiness units by sitting next to Alice.
30
+Eric would gain 24 happiness units by sitting next to Bob.
31
+Eric would gain 100 happiness units by sitting next to Carol.
32
+Eric would gain 51 happiness units by sitting next to David.
33
+Eric would gain 21 happiness units by sitting next to Frank.
34
+Eric would gain 55 happiness units by sitting next to George.
35
+Eric would lose 44 happiness units by sitting next to Mallory.
36
+Frank would lose 48 happiness units by sitting next to Alice.
37
+Frank would gain 91 happiness units by sitting next to Bob.
38
+Frank would gain 8 happiness units by sitting next to Carol.
39
+Frank would lose 66 happiness units by sitting next to David.
40
+Frank would gain 97 happiness units by sitting next to Eric.
41
+Frank would lose 9 happiness units by sitting next to George.
42
+Frank would lose 92 happiness units by sitting next to Mallory.
43
+George would lose 44 happiness units by sitting next to Alice.
44
+George would lose 25 happiness units by sitting next to Bob.
45
+George would gain 17 happiness units by sitting next to Carol.
46
+George would gain 92 happiness units by sitting next to David.
47
+George would lose 92 happiness units by sitting next to Eric.
48
+George would gain 18 happiness units by sitting next to Frank.
49
+George would gain 97 happiness units by sitting next to Mallory.
50
+Mallory would gain 92 happiness units by sitting next to Alice.
51
+Mallory would lose 96 happiness units by sitting next to Bob.
52
+Mallory would lose 51 happiness units by sitting next to Carol.
53
+Mallory would lose 81 happiness units by sitting next to David.
54
+Mallory would gain 31 happiness units by sitting next to Eric.
55
+Mallory would lose 73 happiness units by sitting next to Frank.
56
+Mallory would lose 89 happiness units by sitting next to George.

Loading…
Cancel
Save