-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday15_lib.tal
430 lines (379 loc) · 9.36 KB
/
day15_lib.tal
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
~library/console.lib.tal
~library/string.tal
~library/math.tal
@risk-matrix
[ &_risks $2720 &_risks_end &_w $2 &_h $2 ]
( get-byte -- )
&parse
[ ;&parse/get-byte STA2 ]
#00 ;&_risks_end ;&_risks SUB2 ;&_risks ;memset JSR2
#0000 [ ;&_h STA2 ]
LIT2r :&_risks ( : p* )
#0000 [ ;&parse/w STA2 ]
&parse/loop
LIT2 [ &parse/get-byte $2 ] JSR2
DUP #00 EQU ;&parse/done JCN2
DUP #0a EQU ;&parse/next-row JCN2
LIT '0 SUB ( parse )
STH2rk STA INC2r ( store )
[ ;&parse/w *INC2 ] ( count )
;&parse/loop JMP2
&parse/next-row
POP
[ ;&_h *INC2 ]
[ ;&parse/w LDA2 ] #0000 EQU2 ,&parse/not-started JCN
[ ;&parse/w LDA2 ] [ ;&_w STA2 ]
&parse/not-started
#0000 [ ;&parse/w STA2 ]
;&parse/loop JMP2
&parse/done
POP POP2r
RTN
[ &parse/w $2 ]
( x y -- ptr )
&ptr-at
( x y )
[ ;&_h LDA2 ] MOD2
( x y%h )
[ ;&_w LDA2 ] MUL2 ( x y*w )
SWP2
( y*w x )
[ ;&_w LDA2 ] MOD2
ADD2
;&_risks ADD2
RTN
&ptr-at/outside
POP2 POP2
#ff [ ;&dummy-cell STA ]
;&dummy-cell
RTN
[ &dummy-cell $1 ]
( -- )
&dump
[ ;&_w LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 DBGSHORTDECn SP POP2
[ ;&_h LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 DBGSHORTDEC POP2
#0000 [ ;&_h LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 DO
#0000 [ ;&_w LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 DO
DUP4 SWP2 ;&get-at JSR2
LIT '0 ADD EMIT
SP
LOOP
LF
LOOP
RTN
( x y -- val )
&get-at
DUP4 ;&ptr-at JSR2 LDA
STH ( x y : val )
[ ;&_h LDA2 ] DIV2 ( x y-tile : val )
SWP2 [ ;&_w LDA2 ] DIV2 ( y-tile x-tile : val )
ADD2 ( tile : val )
NIP STHr ( tile8 val )
DEC ADD #09 MOD INC
RTN
%VISITED_SIZE { $7c00 }
@cost-matrix
&init
[ ;risk-matrix/_w LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 [ ;&_w STA2 ]
[ ;risk-matrix/_h LDA2 ] [ ;cost-matrix/scale LDA2 ] MUL2 [ ;&_h STA2 ]
[ ;&_w LDA2 ] DEC2 8// INC2 [ ;&_visited_w STA2 ]
#00 ;&_costlist_end ;&_costlist SUB2 ;&_costlist ;memset JSR2
#00 ;&_visited_end ;&_visited SUB2 ;&_visited ;memset JSR2
#ffff [ ;&_costlist STA2 ]
RTN
( val x y -- )
&set-at
( P< "set 20 >P OVR2 DBGSHORTDECn POP2 SP DBGSHORTDECn SP STH4 DBGSHORTDEC STH4r )
DUP2 [ ;&_h LDA2 ] GEQ2 ;&set-at/outside JCN2
OVR2 [ ;&_w LDA2 ] GEQ2 ;&set-at/outside JCN2
DUP4 ;&mark-visited JSR2
( find end of costlist )
LIT2r :&_costlist ( : cost* )
&set-at/loop
STH2rk LDA2 #ffff EQU2 ,&set-at/at-end JCN
( val x y : cost* )
( is this slot usable? )
STH2rk ( val x y cost* : cost* )
#0004 ADD2 LDA2 ( val x y costval : cost* )
STH2 ( val x y : costval cost* )
ROT2 ( x y val : costval cost* )
STH2k ( x y val : val costval cost* )
ROT2 ROT2 ( val x y : val costval cost* )
STH2r STH2r ( val x y val costval : cost* )
#000a ADD2 ( val costval+10 )
LTH2 ,&set-at/not-free JCN
( val x y : cost* )
STH2r ( val x y cost* )
;&set-at/store JSR2 ( cost * )
POP2
RTN
&set-at/not-free
LIT2r 0006 ADD2r
,&set-at/loop JMP
&set-at/at-end
( bounds check )
;&_costlist_end STH2rk GTH2 ,&set-at/bounds-ok JCN
P< "costlist 20 "bounds 20 "exceeded >P
!
&set-at/bounds-ok
( val x y : cost* )
STH2r ( val x y cost* )
;&set-at/store JSR2
( cost* )
;&set-at/terminate JSR2
RTN
( val x y cost* -- cost* )
&set-at/store
( val x y cost* )
STH2 ( val x y cost* )
SWP2 ( val y x : cost* )
STH2rk STA2 INC2r INC2r ( x )
STH2rk STA2 INC2r INC2r ( y )
STH2rk STA2 INC2r INC2r ( val )
( : cost* )
STH2r ( cost* )
RTN
( cost* )
&set-at/terminate
#ffff SWP2 STA2
RTN
&set-at/outside
POP2 POP2 POP2
RTN
( x y -- byte* bitpos )
&get-visited-ptr
[ ;&_visited_w LDA2 ] MUL2 ( x y*w )
OVR2 STH2 ( x y*w : x )
SWP2 8// ADD2 ;&_visited ADD2 ( byte* : x )
STH2r NIP ( byte* xlow )
#07 AND ( byte* bitpos )
RTN
( x y -- )
&mark-visited
( P< "visit 20 >P OVR2 DBGSHORTDECn SP POP2 DBGSHORTDECn SP )
;&get-visited-ptr JSR2 STH
( byte* : bitpos )
LDAk ( byte* byte : bitpos )
#80 STHr ( byte* byte 80 bitpos )
SFT ( byte* byte bit )
ORA ( byte* byte|bit )
( DBGBYTEn )
ROT ROT ( newbyte byte* )
( DBGSHORT )
STA
RTN
( -- )
&dump-visited
[ ;&_w LDA2 ] DBGSHORTDECn SP POP2
[ ;&_h LDA2 ] DBGSHORTDEC POP2
#0000 [ ;&_h LDA2 ] DO
#0000 [ ;&_w LDA2 ] DO
DUP4 SWP2 ;&get-visited JSR2
LIT '0 ADD EMIT
SP
LOOP
LF
LOOP
RTN
( -- )
&dump-visited-bytes
[ ;&_visited_w LDA2 ] DBGSHORTDECn SP POP2
[ ;&_h LDA2 ] DBGSHORTDEC POP2
#0000 [ ;&_h LDA2 ] DO
#0000 [ ;&_visited_w LDA2 ] DO
( y x )
DUP4 8** SWP2
( y x 8*x y )
;&get-visited-ptr JSR2
( y x byte* bitpos )
POP LDA ( byte )
;print-byte JSR2
SP
LOOP
LF
LOOP
RTN
( x y -- flag )
&get-visited
( LIT 'G EMIT OVR2 DBGSHORTDECn SP POP2 DBGSHORTDECn SP )
;&get-visited-ptr JSR2 STH
( byte* : bitpos )
LDA #80 STHr SFT ( byte mask )
AND ( masked )
#00 NEQ
RTN
( x y -- val )
&get-at
DUP2 [ ;&_h LDA2 ] GEQ2 ,&get-at/outside JCN
OVR2 [ ;&_w LDA2 ] GEQ2 ,&get-at/outside JCN
STH2 STH2 ( : x y )
( scan costlist )
;&_costlist ( cost* : x y )
&get-at/loop
LDA2k #ffff EQU2 ,&get-at/at-end JCN
( cost* : x y )
LDA2k STH2rk NEQ2 ,&get-at/next JCN
( cost* : x y )
DUP2 #0002 ADD2 LDA2 OVR2r STH2r NEQ2 ,&get-at/next JCN
( found match )
( cost* : x y )
#0004 ADD2 LDA2
POP2r POP2r
( cost )
RTN
&get-at/next
#0006 ADD2
,&get-at/loop JMP
&get-at/at-end
( cost* : x y )
POP2 POP2r POP2r
#0000
RTN
&get-at/outside
POP2 POP2
#ffff
RTN
( cost x y -- cost|visitbit )
&add-visited-bit
;&get-visited JSR2
#00 SWP #f0 SFT2
ORA2
RTN
( -- count )
&count
( scan costlist )
;&_costlist ( cost* )
&count/loop
LDA2k #ffff EQU2 ,&count/at-end JCN
#0006 ADD2
,&count/loop JMP
&count/at-end
( cost-end* )
;&_costlist SUB2 #0006 DIV2
RTN
( val x y -- )
&combine-at
STH2k OVR2 STH2 ( val x y : x y )
;&get-visited JSR2 ( val visited? : x y )
,&combine-at/visited JCN
( val : x y )
STH2rk OVR2r STH2r ( val x y : x y )
;risk-matrix/get-at JSR2 #00 SWP
( val riskval : x y )
ADD2 ( val+riskval : x y )
STH2r STH2r ( val+riskval x y )
;&set-at JSR2
RTN
&combine-at/visited
( val : x y )
POP2
POP2r POP2r
RTN
( x y -- )
&flood-from
DUP4 ;&get-at JSR2 STH2 ( : val )
OVR2 INC2 OVR2 STH2rk ROT2 ROT2 ;&combine-at JSR2
OVR2 DEC2 OVR2 STH2rk ROT2 ROT2 ;&combine-at JSR2
OVR2 OVR2 INC2 STH2rk ROT2 ROT2 ;&combine-at JSR2
DEC2 STH2rk ROT2 ROT2 ;&combine-at JSR2
POP2r
RTN
( val -- )
&flood-matching
STH2 ( : val )
( scan costlist )
;&_costlist ( cost* : x y )
&flood-matching/loop
LDA2k #ffff EQU2 ,&flood-matching/at-end JCN
( cost* : val )
DUP2 #0004 ADD2 LDA2 STH2rk NEQ2 ,&flood-matching/next JCN
( found match )
( cost* : val )
LDA2k ( cost* x : val )
OVR2 #0002 ADD2 LDA2 ( cost* x y : val )
;&flood-from JSR2
( cost* : val )
&flood-matching/next
#0006 ADD2
,&flood-matching/loop JMP
&flood-matching/at-end
( cost* : val )
POP2 POP2r
RTN
( -- )
&flood-all
#0000 #0000 #0000 ;&set-at JSR2
#0000 #2000 DO
( DBGSHORTDECn SP ;&count JSR2 DBGSHORTDEC POP2 )
DUP2 ;&flood-matching JSR2
;&is-end-visited JSR2
NOT ,&flood-all/not-done JCN
UNLOOP
RTN
&flood-all/not-done
( ;&dump-visited JSR2 )
( ;&dump JSR2 )
LOOP
RTN
( -- flag )
&is-end-visited
[ ;&_w LDA2 ] DEC2 [ ;&_h LDA2 ] DEC2 ;&get-visited JSR2
RTN
( -- )
&list-dump
LIT2r :&_costlist ( : cost* )
&list-dump/loop
STH2rk LDA2 #ffff EQU2 ,&list-dump/at-end JCN
( : cost* )
STH2rk LDA2k DBGSHORTDECn SP POP2
#0002 ADD2 LDA2k DBGSHORTDECn SP POP2
#0002 ADD2 LDA2k ;print-short-no0x JSR2
POP2
LF
LIT2r 0006 ADD2r
,&list-dump/loop JMP
&list-dump/at-end
( : cost* )
POP2r
RTN
( -- )
&dump
[ ;&_w LDA2 ] DBGSHORTDECn SP POP2
[ ;&_h LDA2 ] DBGSHORTDEC POP2
#0000 [ ;&_h LDA2 ] DO
#0000 [ ;&_w LDA2 ] DO
( y x )
DUP4 ( y x y x )
SWP2
( y x x y )
DUP4 ;&get-visited JSR2
NOT ,&dump/not-visited JCN
;&get-at JSR2
( x y cost )
;print-short-no0x JSR2
,&dump/next JMP
&dump/not-visited
( y x x y )
POP2 POP2
P< 20 20 20 20 >P
&dump/next
SP
LOOP
LF
LOOP
RTN
( -- end-val )
&get-end
[ ;&_w LDA2 ] DEC2 [ ;&_h LDA2 ] DEC2 ;&get-at JSR2
RTN
( -- risk )
&get-end-cost
;&get-end JSR2
RTN
[
&_w $2 &_h $2 &_visited_w $2
&_visited VISITED_SIZE &_visited_end
&scale 0001
&_costlist
|ff00 &_costlist_end
]