-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13p2.py
More file actions
executable file
·59 lines (42 loc) · 1.44 KB
/
day13p2.py
File metadata and controls
executable file
·59 lines (42 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import re
from itertools import permutations
with open('input/day13.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
pat = re.compile(
r'(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)'
)
people = list()
happiness = dict()
max_happiness = 0
for line in data:
m = re.search(pat, line)
if not m:
raise RuntimeError('Bad line')
person1, person2, change, units = \
m.group(1), m.group(4), m.group(2), int(m.group(3))
if person1 not in people:
people.append(person1)
if person2 not in people:
people.append(person2)
if change == 'lose':
units = -units
person1_index = people.index(person1)
person2_index = people.index(person2)
happiness[(person1_index, person2_index)] = units
people.append('me')
my_index = people.index('me')
for person in people:
person_index = people.index(person)
happiness[(my_index, person_index)] = 0
happiness[(person_index, my_index)] = 0
for order in permutations(range(len(people))):
total_happiness = 0
for pair in zip(order[:-1], order[1:]) + [(order[-1], order[0])]:
total_happiness += happiness[pair]
order = order[::-1] # Now go the other way!
for pair in zip(order[:-1], order[1:]) + [(order[-1], order[0])]:
total_happiness += happiness[pair]
if max_happiness < total_happiness:
max_happiness = total_happiness
print max_happiness