-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21vis.py
More file actions
267 lines (236 loc) · 8.16 KB
/
day21vis.py
File metadata and controls
267 lines (236 loc) · 8.16 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Problem statement: https://adventofcode.com/2024/day/16
from py5 import Sketch # , Py5Font
from aocd import data
from .day21 import day_title, numeric_keypad, directional_keypad, Keypad
from statemachine import StateMachine, State
from functools import cache
WIDTH = 1920
HEIGHT = 1080
PADDING = 36
SILVER = (153, 153, 204)
GOLD = (230, 230, 94)
TEXT_COLOR = (204, 204, 204)
BACKGROUND = (15, 15, 35)
BACKGROUND_2 = (25, 25, 55)
WHITE = (255, 255, 255)
WALL_COLOR = (200, 200, 200)
FONT_SIZE = 30
text = data # aocd magic, gets correct input by inspecting filepath for year/day
# copy pasted from solution - need to find actual sequences,
# not just their lengths
@cache
def best_controls_directional(code: str, level: int):
"""Find shortest sequence to enter `code` on a directional keypad
through `level` layers of directional keypads"""
seq = ""
for basic_programs in directional_keypad.controls_for_code(code):
if level == 0:
seq += min((program for program in basic_programs), key=len)
else:
seq += min(
(
best_controls_directional(program, level - 1)
for program in basic_programs
),
key=len,
)
return seq
def best_controls_numeric(code: str, levels: int):
"""Find shortest sequence length to enter `code` on a numeric keypad
through `levels` directional keypads"""
seq = ""
for basic_programs in numeric_keypad.controls_for_code(code):
seq += min(
(
best_controls_directional(program, level=levels - 1)
for program in basic_programs
),
key=len,
)
return seq
class KeypadState:
def __init__(self, keypad: Keypad):
self.keypad = keypad
self.r0, self.c0 = keypad.button_positions["A"]
self.r, self.c = keypad.button_positions["A"]
self.pushing = None
def execute(self, command: str | None):
self.r0, self.c0 = self.r, self.c
self.pushing = None
if command is None:
return
elif command == ">":
self.r, self.c = self.r0, self.c0 + 1
elif command == "<":
self.r, self.c = self.r0, self.c0 - 1
elif command == "^":
self.r, self.c = self.r0 - 1, self.c0
elif command == "v":
self.r, self.c = self.r0 + 1, self.c0
else:
self.pushing = self.keypad.lines[self.r][self.c]
return self.keypad.lines[self.r][self.c]
return None
def reset(self):
self.r0, self.c0 = self.keypad.button_positions["A"]
self.r, self.c = self.keypad.button_positions["A"]
class VisMachine(StateMachine):
# data fields
started = False
ticks = 0
duration = 0
done = 1.0
total_1 = 0
total_2 = 0
is_finished = False
# animation data
keypads = [
KeypadState(directional_keypad),
KeypadState(directional_keypad),
KeypadState(directional_keypad),
KeypadState(numeric_keypad),
]
codes = text.split()
code_index = -1
code = ""
entered_code = ""
sequence = ""
seq_index = -1
code_done = False
all_codes_done = False
# animation stages
init = State(initial=True)
begin_code = State()
enter_commands = State()
finish_code = State()
end = State(final=True)
transition = (
init.to(begin_code)
| begin_code.to(enter_commands)
| enter_commands.to.itself(unless="code_done")
| enter_commands.to(finish_code)
| finish_code.to(begin_code, unless="all_codes_done")
| finish_code.to(end)
)
def on_enter_begin_code(self):
self.duration = 1
self.code_index += 1
self.code = self.codes[self.code_index]
self.entered_code = ""
self.seq_index = -1
self.sequence = best_controls_numeric(self.code, levels=2)
for k in self.keypads:
k.reset()
self.all_codes_done = self.code_index == len(self.codes) - 1
def on_enter_enter_commands(self):
self.duration = 30
self.seq_index += 1
command = self.sequence[self.seq_index]
self.keypads[0].pushing = command
for keypad in self.keypads[1:]:
command = keypad.execute(command)
if command:
self.entered_code += command
self.code_done = self.entered_code == self.code
def on_enter_finish_code(self):
self.duration = 180
# timer to change stages
def tick(self):
if not self.started:
return
self.ticks += 1
if self.ticks >= self.duration and not self.current_state.final:
self.ticks = 0
self.done = 0.0
self.transition()
elif self.duration > 0:
self.done = self.ticks / self.duration
data = VisMachine()
W = WIDTH // 4
imgs = []
class VisSketch(Sketch):
def settings(self):
self.size(WIDTH, HEIGHT)
self.smooth()
def setup(self):
global background
self.window_title(f"Advent of Code 2024 - Day 21 - {day_title}")
# prepare and measure font
font = self.create_font("Liberation Mono", FONT_SIZE)
self.text_font(font)
self.text_size(FONT_SIZE)
self.rect_mode(self.CENTER)
self.text_align(self.CENTER, self.CENTER)
# background = self.create_graphics(WIDTH, HEIGHT)
# background.begin_draw()
# self.draw_walls(gr=background)
# background.end_draw()
imgs.extend(
(
self.load_image("christmas.jpg"),
self.load_image("frost.jpg"),
self.load_image("radioactive.jpg"),
self.load_image("space.jpg"),
)
)
data.add_listener(self)
def mouse_clicked(self):
if data.started:
title = "-".join(day_title.lower().split())
self.save_frame(f"2024-day21-{title}-{self.frame_count:03d}.png")
data.started = not data.started
def draw_keypad(self, x, y, keypadstate, cursor=True):
self.push()
self.fill(*BACKGROUND_2)
self.rect(x, y, 300, 400)
self.fill(*TEXT_COLOR, 128)
self.rect(x, y, 300, 400)
for r, line in enumerate(keypadstate.keypad.lines):
for c, char in enumerate(line):
if char == "X":
continue
if char == keypadstate.pushing:
self.fill(*SILVER)
else:
self.fill(*TEXT_COLOR)
self.rect(x - 100 + 100 * c, y - 150 + 100 * r, 80, 80)
self.fill(*BACKGROUND_2)
self.text(char, x - 100 + 100 * c, y - 150 + 100 * r)
if cursor:
r = self.lerp(keypadstate.r0, keypadstate.r, data.done)
c = self.lerp(keypadstate.c0, keypadstate.c, data.done)
self.fill(0, 255, 0, 64)
self.circle(x - 100 + 100 * c, y - 150 + 100 * r + 20, 20)
self.pop()
def draw_entered_code(self):
self.push()
self.fill(*TEXT_COLOR, 128)
self.rect(7 * W / 2, 200, 300, 100)
if data.current_state == VisMachine.finish_code:
self.fill("#2ab34b")
else:
self.fill(*BACKGROUND_2)
self.text_size(80)
self.text_align(self.LEFT, self.CENTER)
self.text(data.entered_code, 7 * W / 2 - 100, 200)
if len(data.entered_code) < len(data.code):
self.fill(90.0)
n = len(data.entered_code)
self.text(" " * n + data.code[n:], 7 * W / 2 - 100, 200)
self.pop()
def draw(self):
data.tick()
self.background(*BACKGROUND)
for i, img in enumerate(imgs):
self.image(img, i * W, 0)
self.push()
self.stroke_weight(10)
self.stroke(*BACKGROUND_2)
for i in range(1, 4):
self.line(W * i, 0, W * i, HEIGHT)
self.pop()
self.draw_keypad(W / 2, 500, data.keypads[0], cursor=False)
self.draw_keypad(3 * W / 2, 500, data.keypads[1])
self.draw_keypad(5 * W / 2, 500, data.keypads[2])
self.draw_keypad(7 * W / 2, 500, data.keypads[3])
self.draw_entered_code()