-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.py
219 lines (196 loc) · 6.58 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import heapq
from pathlib import Path
TEST_INPUT = """#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#""".splitlines()
COORDINATE_TYPE = tuple[int, int]
GRID_TYPE = dict[COORDINATE_TYPE, str]
DIRECTIONS = {
">": (1, 0),
"<": (-1, 0),
"^": (0, -1),
"v": (0, 1),
}
def parse_input(
puzzle: list[str],
) -> tuple[GRID_TYPE, COORDINATE_TYPE, COORDINATE_TYPE]:
start = None
end = None
grid: GRID_TYPE = {}
for y, row in enumerate(puzzle):
for x, char in enumerate(row):
if y == 0 and not start:
if char == ".":
start = (x, y)
if y == len(puzzle) - 1 and not end:
if char == ".":
end = (x, y)
grid[x, y] = char
return grid, start, end
def make_move(
grid: GRID_TYPE, person: COORDINATE_TYPE, width: int, height: int
) -> list[tuple[GRID_TYPE, COORDINATE_TYPE]]:
assert grid[person] == ".", (grid, person)
results = []
new_grid: GRID_TYPE = {}
for (x, y), chars in grid.items():
if chars == ".":
# nothing leaves this place
continue
elif chars == "#":
new_grid[x, y] = chars
continue
for char in chars:
dx, dy = DIRECTIONS[char]
if grid[x + dx, y + dy] == "#":
# warp!
if dx == -1:
# left wall
save_to_grid(new_grid, width - 2, y, char)
elif dx == 1:
# right wall
save_to_grid(new_grid, 1, y, char)
elif dy == -1:
# top wall
save_to_grid(new_grid, x, height - 2, char)
else:
assert dy == 1, (dx, dy)
save_to_grid(new_grid, x, 1, char)
else:
save_to_grid(new_grid, x + dx, y + dy, char)
# print(new_grid)
# now let's put our dots in
for x in range(width):
for y in range(height):
if (x, y) not in new_grid:
new_grid[x, y] = "."
if new_grid[person] == ".":
# not moving is valid
# print(f'not moving from {person} is valid')
results.append((new_grid.copy(), person))
x, y = person
for dx, dy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
if (dx, dy) not in grid:
continue
if new_grid[dx, dy] != ".":
continue
# print(f'moving from {person} to {(dx, dy)} is valid')
# this move is valid
results.append((new_grid.copy(), (dx, dy)))
return results
def save_to_grid(grid: GRID_TYPE, x: int, y: int, char: str):
assert len(char) == 1
try:
grid[x, y] += char
except KeyError:
grid[x, y] = char
def part_one(puzzle: list[str]) -> int:
best_turns = 750
grid, start, end = parse_input(puzzle)
width = len(puzzle[0])
height = len(puzzle)
queue = [(0, start, grid)]
heapq.heapify(queue)
iterations = 0
# states_seen = set(
# (
# start,
# frozenset(grid.items()),
# )
# )
# print('trying to get to ', end)
states_seen: set[tuple[int, COORDINATE_TYPE]] = set()
while queue:
try:
turns, position, new_grid = heapq.heappop(queue)
except IndexError:
return best_turns
if position == end:
# print(f'\n\nmade it to the end in {turns} moves')
best_turns = min(best_turns, turns)
continue
if turns >= best_turns:
continue
set_key = (turns, position)
if set_key in states_seen:
continue
states_seen.add(set_key)
for newer_grid, new_position in make_move(
new_grid, position, width=width, height=height
):
# print(f"moved from {position} to {new_position}")
# display_grid(newer_grid, position)
heapq.heappush(queue, (turns + 1, new_position, newer_grid))
iterations += 1
print(f"{iterations}\t{len(queue)}\t{turns}\t{best_turns}", end="\r")
print("")
return best_turns
def part_two(puzzle: list[str], part_one_score: int) -> int:
best_turns = 750
grid, end, start = parse_input(puzzle)
width = len(puzzle[0])
height = len(puzzle)
for _ in range(part_one_score):
grid, _ = make_move(grid, end, width, height)[0]
queue = [(part_one_score, start, grid)]
heapq.heapify(queue)
iterations = 0
# states_seen = set(
# (
# start,
# frozenset(grid.items()),
# )
# )
# print('trying to get to ', end)
states_seen: set[tuple[int, COORDINATE_TYPE]] = set()
while queue:
try:
turns, position, new_grid = heapq.heappop(queue)
except IndexError:
return best_turns
if position == end:
# print(f'\n\nmade it to the end in {turns} moves')
best_turns = min(best_turns, turns)
continue
if turns >= best_turns:
continue
set_key = (turns, position)
if set_key in states_seen:
continue
states_seen.add(set_key)
for newer_grid, new_position in make_move(
new_grid, position, width=width, height=height
):
# print(f"moved from {position} to {new_position}")
# display_grid(newer_grid, position)
heapq.heappush(queue, (turns + 1, new_position, newer_grid))
iterations += 1
print(f"{iterations}\t{len(queue)}\t{turns}\t{best_turns}", end="\r")
print("")
return best_turns
def display_grid(grid: GRID_TYPE, person: COORDINATE_TYPE):
max_x = max(x for x, _ in grid)
max_y = max(y for _, y in grid)
for y in range(max_y + 1):
for x in range(max_x + 1):
if (x, y) == person:
print("P", end="")
elif (length := len(grid[x, y])) > 1:
print(length, end="")
else:
print(grid[x, y], end="")
print("")
def main():
part_one_result = part_one(TEST_INPUT)
assert part_one_result == 18, part_one_result
print("main puzzle: go")
puzzle = Path("day24.txt").read_text().splitlines()
part_one_result = part_one(puzzle=puzzle)
print(part_one_result)
print("here goes part two")
print(part_two(puzzle, part_one_result))
if __name__ == "__main__":
main()