summary refs log tree commit diff stats
path: root/day13.py
blob: 1f5bd23a222762bcad363d91030c2f54dca2a9ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python

from collections import defaultdict
from itertools import permutations

costs = defaultdict(lambda: defaultdict(int))

with open('day13.txt') as data:
    for line in data:
        line = line.strip('.\n').split()
        costs[line[0]][line[-1]] = int(line[3]) * (-1 if line[2] == 'lose' else 1)

def calculate_cost(ordering):
    return sum(costs[ordering[i-1]][ordering[i]] + costs[ordering[i]][ordering[i-1]]for i in range(len(ordering)))

# part 1
print(max(calculate_cost(order) for order in permutations(costs.keys())))

# part 2
for key in set(costs.keys()):
    costs[key]['Myself'] = 0
    costs['Myself'][key] = 0

print(max(calculate_cost(order) for order in permutations(costs.keys())))