#!/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())))