-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
139 lines (110 loc) · 3.7 KB
/
day18.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import string
from dataclasses import dataclass
from typing import Tuple, FrozenSet, NamedTuple
from collections import deque, defaultdict
from utils import neighbours
import heapq
KEYS = set(string.ascii_lowercase)
DOORS = set(string.ascii_uppercase)
def find_start_pos(world):
starts = []
for row in range(len(world)):
for col in range(len(world[row])):
if world[row][col] == "@":
starts.append((row, col))
return tuple(starts)
def find_num_keys(world):
keys = 0
for row in range(len(world)):
for col in range(len(world[row])):
if world[row][col] in KEYS:
keys += 1
return keys
@dataclass
class State:
pos: Tuple[int, int]
keys: FrozenSet[set]
num_steps: int
def a(world):
def w(pos):
return world[pos[0]][pos[1]]
start_pos = find_start_pos(world)[0]
total_keys = find_num_keys(world)
start_state = State(start_pos, frozenset(), 0)
visited = {(start_state.pos, start_state.keys)}
queue = deque([start_state])
while queue:
state = queue.popleft()
if len(state.keys) == total_keys:
return state.num_steps
for neighbour in neighbours(state.pos):
keys = state.keys
if w(neighbour) == "#":
continue
elif w(neighbour) in KEYS:
keys = keys | {w(neighbour)}
elif w(neighbour) in DOORS:
if w(neighbour).lower() not in state.keys:
continue
k = (neighbour, keys)
if k not in visited:
visited.add(k)
queue.append(State(neighbour, keys, state.num_steps + 1))
def find_all_keys(world):
keys = {}
for row in range(len(world)):
for col in range(len(world[row])):
if world[row][col].islower():
keys[world[row][col]] = row, col
return keys
def reachable_keys_for_pos(start, world):
def w(pos):
return world[pos[0]][pos[1]]
visited = {start}
queue = deque([start])
result = []
while queue:
pos = queue.popleft()
if w(pos) in KEYS:
result.append(w(pos))
for neighbour in neighbours(pos):
if w(neighbour) != "#" and neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
return frozenset(result)
def solve_quadrant(world, start_pos, keys_in_quadrant):
def w(pos):
return world[pos[0]][pos[1]]
start_state = State(start_pos, frozenset(), 0)
visited = {(start_state.pos, start_state.keys)}
queue = deque([start_state])
while queue:
state = queue.popleft()
if len(state.keys) == len(keys_in_quadrant):
return state.num_steps
for neighbour in neighbours(state.pos):
keys = state.keys
if w(neighbour) == "#":
continue
elif w(neighbour) in KEYS:
keys = keys | {w(neighbour)}
elif w(neighbour) in DOORS:
required_key = w(neighbour).lower()
if required_key in keys_in_quadrant and required_key not in state.keys:
continue
k = (neighbour, keys)
if k not in visited:
visited.add(k)
queue.append(State(neighbour, keys, state.num_steps + 1))
def b(world):
starts = find_start_pos(world)
keys_per_start = [reachable_keys_for_pos(start, world) for start in starts]
return sum(
solve_quadrant(world, start, keys)
for start, keys in zip(starts, keys_per_start)
)
def main():
world = open("input18.txt").read().splitlines()
print(b(world))
if __name__ == "__main__":
main()