In [50]:
import re
from collections import defaultdict
from itertools import permutations
_PARSE = re.compile("([A-Za-z]+) would (gain|lose) ([0-9]+) happiness units by sitting next to ([A-Za-z]+).")
def count_happines(units, order):
p = order[0]
v_l = units[p][order[-1]]
for c in order[1:]:
v_l += int(units[p][c])
p = c
p = order[-1]
v_r = units[p][order[0]]
for c in order[::-1][1:]:
v_r += int(units[p][c])
p = c
return v_r + v_l
def happiness(data, addme=False):
units = defaultdict(dict)
for d in data:
name1, gain_loss, points, name2 = _PARSE.match(d).groups()
points = int(points) if gain_loss == 'gain' else -int(points)
units[name1][name2] = points
if addme:
for name in list(units.keys()):
units['me'][name] = 0
units[name]['me'] = 0
names = list(units.keys())
max_perm = tuple(names)
max_value = count_happines(units, max_perm)
for perm in permutations(units.keys()):
value = count_happines(units, perm)
if value > max_value:
max_perm = perm
max_value = value
return max_value, max_perm
In [51]:
# Test example
example = [
"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."
]
happiness(example)
Out[51]:
In [52]:
# Test input
with open('day13/input.txt', 'rt') as fd:
print(happiness(fd))
In [53]:
with open('day13/input.txt', 'rt') as fd:
print(happiness(fd, addme=True))
In [ ]: