aoc2015/d13/run.py

27 lines
597 B
Python

import sys, os
from collections import defaultdict as dd
from itertools import permutations as PERM
N:set[str] = set()
D:dict[tuple[str,str],int] = dd(int)
for l in sys.stdin.readlines():
[a,s,b] = l[:-1].split(" ")
D[(a,b)] += int(s)
D[(b,a)] += int(s)
N.add(a)
N.add(b)
if len(sys.argv)>1 and sys.argv[1]=="2":
# add you
for a in N:
D[(a,'Y')] = 0
D[('Y',a)] = 0
N.add('Y')
def score(chain:list[str]) -> int:
return sum(D[(a,b)] for (a,b) in zip(chain, chain[1:])) \
+ D[(chain[0],chain[-1])]
print(max(score(p) for p in PERM(N)))