-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday13.py
More file actions
26 lines (19 loc) · 725 Bytes
/
day13.py
File metadata and controls
26 lines (19 loc) · 725 Bytes
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
from collections import defaultdict
from itertools import permutations
def parse_line(line):
a, _, gl, n, *_, b = line.split()
return a, b[:-1], gl, int(n)
def create_relations(data):
relations = defaultdict(lambda: defaultdict(int))
for a, b, gl, n in data:
relations[a][b] = n if gl == 'gain' else -n
return relations
solve = lambda people, rels: (
max(sum(rels[a][b] + rels[b][a]
for a, b in zip(pp, list(pp[1:]) + [pp[0]]))
for pp in permutations(people)))
data = map(parse_line, open("inputs/13.txt").read().splitlines())
relations = create_relations(data)
people = set(relations.keys())
print(solve(people, relations))
print(solve(people | {'me'}, relations))