-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.py
47 lines (38 loc) · 1.24 KB
/
day24.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
from copy import copy
from collections import defaultdict, deque
from timeit import default_timer as timer
def moves(state, ports):
strength, visited, current = state
result = []
for i, port in enumerate(ports):
if i in visited or (port[0] != current and port[1] != current):
continue
new_visited = copy(visited)
new_visited.add(i)
new_current = port[0] if port[1] == current else port[1]
result.append((strength + port[0] + port[1], new_visited, new_current))
return result
ports = []
for line in open("input/24.txt"):
a, b = map(int, line.strip().split("/"))
ports.append((a, b))
start = timer()
queue = deque()
visited = set()
# Strength, visited, current
state = (0, set(), 0)
queue.append(state)
max_strength = defaultdict(int)
while queue:
curr = queue.pop()
if curr[0] > max_strength[len(curr[1])]:
max_strength[len(curr[1])] = curr[0]
for move in moves(curr, ports):
move_tuple = (tuple(sorted(move[1])), move[2])
if move_tuple not in visited:
visited.add(move_tuple)
queue.append(move)
print(max(max_strength.values()))
print(max_strength[max(max_strength.keys())])
end = timer()
print(f'Took {end - start} seconds.')