-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
66 lines (53 loc) · 1.99 KB
/
day16.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from functools import lru_cache
import sys
sys.setrecursionlimit(100000000)
data = open("input.txt", "r").read().strip().split("\n")
def parse_input(data: list[str]):
graph = {}
for line in data:
name = line.split(" ")[1]
flow_rate = int(line[line.index("=") + 1 : line.index(";")])
neighbours = line[line.index(";") + 24 :].strip().replace(" ", "").split(",")
graph[name] = (flow_rate, neighbours)
return graph
def part_one(data: list[str]):
graph = parse_input(data)
print(graph)
@lru_cache(maxsize=None)
def helper(node, time_left, opened: frozenset):
if time_left <= 0:
return 0
else:
results = [helper(n, time_left - 1, opened) for n in graph[node][1]]
if node not in opened and graph[node][0] > 0:
val = (time_left - 1) * graph[node][0]
_opened = set(opened)
_opened.add(node)
results += [helper(node, time_left - 1, frozenset(_opened)) + val]
return max(results)
return helper("AA", 30, frozenset())
def part_two(data: list[str]):
graph = parse_input(data)
@lru_cache(maxsize=None)
def helper(node, time_left, opened: frozenset, player_turn):
if time_left <= 0:
if player_turn:
return helper("AA", 26, opened, False)
else:
return 0
else:
results = [
helper(n, time_left - 1, opened, player_turn) for n in graph[node][1]
]
if node not in opened and graph[node][0] > 0:
val = (time_left - 1) * graph[node][0]
_opened = set(opened)
_opened.add(node)
results += [
helper(node, time_left - 1, frozenset(_opened), player_turn) + val
]
return max(results)
return helper("AA", 26, frozenset(), True)
if __name__ == "__main__":
print(part_one(data))
print(part_two(data))