-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16_part1.py
49 lines (35 loc) · 1.1 KB
/
16_part1.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
44
45
46
47
48
49
from collections import deque
#inspired by some odd mix of reddit comments
lines = open(0).readlines()
data = {}
for i in lines:
i = i.rstrip("\n").replace("Valve ", "").replace("has flow rate=", "").replace(" tunnels lead to valves ", "").replace(" tunnel leads to valve ", "").split(";")
k = i[0].split(" ")[0]
v = (int(i[0].split(" ")[1]), i[1].split(", "))
data[k] = v
queue = deque([("AA", 0, 0, ())])
visited = set()
pressure = 0
last = 0
while queue:
val = queue.popleft()
passed = val[2]
accum = val[1]
opened = val[3]
valve = val[0]
if passed == 30:
pressure = max(pressure, accum)
last = pressure
continue
if (opened, valve) in visited:
continue
visited.add((opened, valve))
tempaccum = accum
for i in opened:
tempaccum += data[i][0]
if data[valve][0] != 0:
if valve not in opened:
queue.append((valve, tempaccum, passed+1, tuple(list(opened)+[valve])))
for i in data[valve][1]:
queue.append((i, tempaccum, passed+1, opened))
print(last)