-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
43 lines (35 loc) · 940 Bytes
/
day13.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
import os
import re
import itertools
f = open('input/{}.txt'.format(os.path.splitext(os.path.basename(__file__))[0]),'r')
content = f.read()
f.close()
P = "^(?P<f>[A-Z]).+(?P<a>gain|lose) (?P<n>\d+).+(?P<t>[A-Z]).+\.$"
r = re.compile(P)
def f(p=[]):
u = True if p else False
d = {}
for c in content.split('\n'):
m = r.match(c)
if(m):
if(m.group('f') not in p):
p.append(m.group('f'))
if(u):
d[(m.group('f'),'O')] = 0
d[('O',m.group('f'))] = 0
n = int(m.group('n')) * (-1 if m.group('a') == "lose" else 1)
d[(m.group('f'),m.group('t'))] = n
t = []
x = 0
hv = 0
ips = list(itertools.permutations(p,len(p)))
for ip in ips:
t.append(0)
for i in range(len(ip)):
t[x] += d[(ip[i],ip[i-1])] + d[(ip[i],ip[i+(-len(ip)+1 if i == len(ip)-1 else 1)])]
hv = t[x] if(hv < t[x]) else hv
x+=1
return hv
print("Part 1 : {}".format(f()))
print("Part 2 : {}".format(f(['O'])))