-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17vis.py
More file actions
157 lines (142 loc) · 4.96 KB
/
day17vis.py
File metadata and controls
157 lines (142 loc) · 4.96 KB
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
# Problem statement: https://adventofcode.com/2023/day/17
from util.inputs import movechars_dr_dc
import math
from year2023.day17 import search, CrucibleCity, example_input
from matplotlib import pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Circle
from aocd import get_data
def path_to_xydata(path, y0, x0):
x, y = x0, y0
xx, yy = [x], [y]
for char in path:
dy, dx = movechars_dr_dc[char]
x += dx
y += dy
xx.append(x)
yy.append(y)
return dict(xdata=xx, ydata=yy)
def run():
# text_input = example_input
text_input = get_data(year=2023, day=17)
fig, (ax1, ax2) = plt.subplots(
nrows=1, ncols=2, figsize=(16, 9), facecolor="#0f0f23", dpi=80
)
font = dict(family="monospace", size=20)
ax1.set_title("Part 1", color="#8a8aba", **font)
ax2.set_title("Part 2", color="#f0f062", **font)
ax1.axis("off")
ax2.axis("off")
city_1 = CrucibleCity(text_input, 1, 3)
city_2 = CrucibleCity(text_input, 4, 10)
ax1.imshow(city_1.costs, interpolation="none", cmap="gray", aspect="auto")
ax2.imshow(city_1.costs, interpolation="none", cmap="gray", aspect="auto")
h1 = [[math.nan] * len(city_1.costs[0]) for _ in city_1.costs]
h2 = [[math.nan] * len(city_1.costs[0]) for _ in city_1.costs]
im1 = ax1.imshow(h1, cmap="viridis", vmin=0, vmax=1000)
im2 = ax2.imshow(h2, cmap="viridis", vmin=0, vmax=1000)
ax1.add_patch(
Circle(
(city_1.start[0], city_1.start[1]),
radius=0.3,
facecolor="C1",
lw=1,
)
)
ax1.add_patch(
Circle(
(city_1.finish[0], city_1.finish[1]),
radius=0.3,
facecolor="C1",
lw=1,
)
)
ax2.add_patch(
Circle(
(city_2.start[0], city_2.start[1]),
radius=0.3,
facecolor="C1",
lw=1,
)
)
ax2.add_patch(
Circle(
(city_2.finish[0], city_2.finish[1]),
radius=0.3,
facecolor="C1",
lw=1,
)
)
(trail1,) = ax1.plot([], [], "-", lw=2, color="C1")
(trail2,) = ax2.plot([], [], "-", lw=2, color="C1")
search1 = search(city_1, yield_search_states=True)
search2 = search(city_2, yield_search_states=True)
steps1 = 0
steps2 = 0
done1, done2 = False, False
def animate(i):
nonlocal search1, search2, h1, h2, steps1, steps2, done1, done2
bestcost1, bestcost2 = 0, 0
state1, state2 = None, None
data1, data2 = None, None
if i == 0:
search1 = search(city_1, yield_search_states=True)
trail1.set(xdata=[], ydata=[])
search2 = search(city_2, yield_search_states=True)
trail2.set(xdata=[], ydata=[])
elif i > 30:
if not done1:
iterations_per_frame = max(1, math.ceil(steps1 * 0.01))
for _ in range(iterations_per_frame):
steps1 += 1
try:
elem1 = next(search1)
except StopIteration:
done1 = True
break
state1, priority, bestcost1, len_queue = elem1
x, y = state1.c, state1.r
if math.isnan(h1[y][x]) or h1[y][x] > state1.cost:
h1[y][x] = state1.cost
ax1.set_title(
f"Part 1: Best cost = {bestcost1}",
color="#8a8aba",
**font,
)
data1 = path_to_xydata(state1.path, *city_1.start)
im1.set_array(h1)
trail1.set(**data1)
if not done2:
iterations_per_frame = max(1, math.ceil(steps2 * 0.01))
for _ in range(iterations_per_frame):
steps2 += 1
try:
elem2 = next(search2)
state2, priority, bestcost2, len_queue = elem2
x, y = state2.c, state2.r
if math.isnan(h2[y][x]) or h2[y][x] > state2.cost:
h2[y][x] = state2.cost
except StopIteration:
done2 = True
break
ax2.set_title(
f"Part 2: Best cost = {bestcost2}",
color="#f0f062",
**font,
)
im2.set_array(h2)
data2 = path_to_xydata(state2.path, *city_2.start)
trail2.set(**data2)
return (trail1, trail2, im1, im2)
fig.tight_layout()
ani = animation.FuncAnimation(
fig,
animate,
interval=50,
blit=False,
# frames=24 * 27,
# repeat_delay=3000,
)
# writer = animation.FFMpegWriter(fps=24, metadata=dict(artist="me"), bitrate=1800)
# ani.save("day_17.mp4", writer=writer)
plt.show()