-
Notifications
You must be signed in to change notification settings - Fork 1
/
day16_lib.tal
472 lines (414 loc) · 9.3 KB
/
day16_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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
~library/console.lib.tal
~library/string.tal
~library/math.tal
%CELLSIZE { 0008 }
@decoder
[ &_byte $1 &_shift $1 &_counter $2
&_stack $1000 &_stackend
&_stacktop $2
]
( get-byte -- )
&set-input
[ ;&get-byte/addr STA2 ]
#ff [ ;&_shift STA ]
#0000 [ ;&_counter STA2 ]
;&_stack [ ;&_stacktop STA2 ]
RTN
( -- 0-1-or-ff )
&get-bit
[ ;&_shift LDA ] #ff NEQ ,&get-bit/got-byte JCN
;&next-byte JSR2
&get-bit/got-byte
[ ;&_byte LDA ]
[ ;&_shift LDA ] SFT ( shifted )
#01 AND ( 0-or-1 )
[ ;&_shift *DEC ]
[ ;&_counter *INC2 ]
RTN
&next-byte
;&get-byte JSR2 ( char )
DUP #00 NEQ OVR #0a NEQ AND ,&next-byte/not-eof JCN
P< "read_past_end >P
!
&next-byte/not-eof
DUP LIT 'A LTH ,&next-byte/number JCN
( map 'A into '0+10 )
#07 SUB ( number-char )
&next-byte/number
LIT '0 SUB ( byte )
#03 [ ;&_shift STA ]
[ ;&_byte STA ]
RTN
&get-byte
LIT2 [ &get-byte/addr $2 ] JMP2
( -- number8 )
&read-version
&read-type
;&get-bit JSR2 #20 SFT
;&get-bit JSR2 #10 SFT ORA
;&get-bit JSR2 ORA
RTN
( -- is-last number8 )
&read-digit
;&get-bit JSR2 NOT ( is-last )
;&get-bit JSR2 #30 SFT
;&get-bit JSR2 #20 SFT ORA
;&get-bit JSR2 #10 SFT ORA
;&get-bit JSR2 ORA
( is-last number8 )
RTN
( out* -- )
&read-value
;&tmp #0000 ;short-to-64 JSR2
&read-value/next
;&tmp #10 ;mul64-byte JSR2
;&read-digit JSR2
SWP STH
( digit8 : is-last )
;&tmp ROT ( tmp* digit8 )
;add64-byte JSR2
STHr ,&read-value/done JCN
,&read-value/next JMP
&read-value/done
( out* )
( ;short-to-64 JSR2 )
;&tmp ;copy64 JSR2
RTN
( -- length )
&read-length15
#0000
;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
RTN
( -- count )
&read-count11
#0000
;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
2** ;&get-bit JSR2 ORA
RTN
( type -- char )
&lookup-op
#00 SWP ;&op-lookup ADD2 LDA
RTN
[ &op-lookup '+ '* 'm 'M '# '> '< '= ]
( type -- )
&print-op
;&lookup-op JSR2 EMIT
RTN
( -- )
&print-tree
#0000
;&print-tree-indent JSR2
RTN
( indent -- )
&print-tree-indent
( indent )
;&read-version JSR2 POP
;&read-type JSR2 STH ( indent : type )
DUP2 #0000 EQU2 ,&pt/no-indent JCN
#0000 OVR2 DO
#20 EMIT
LOOP
&pt/no-indent
STHr
( indent type )
DUP #04 EQU ,&print-tree/is-lit JCN
;&get-bit JSR2 ( indent type has-count )
,&print-tree/has-count JCN
,&print-tree/has-length JMP
&print-tree/is-lit
( indent type )
POP POP2
;&tmp ;&read-value JSR2
;&tmp ;print64/no-pad JSR2 LF
RTN
&print-tree/has-count
( indent type )
;&print-op JSR2
LF
;&read-count11 JSR2 ( indent count )
&print-tree/count-more
DUP2 #0000 EQU2 ,&print-tree/count-done JCN
( indent count )
OVR2 INC2 ;&print-tree-indent JSR2
( indent count )
DEC2
,&print-tree/count-more JMP
&print-tree/count-done
( indent 0 )
POP2
POP2
RTN
&print-tree/has-length
( indent type )
;&print-op JSR2
LF
;&read-length15 JSR2 [ ;&_counter LDA2 ] ADD2
( indent end )
&print-tree/length-more
( indent end )
DUP2 [ ;&_counter LDA2 ] LEQ2 ,&print-tree/length-done JCN
( indent end )
OVR2 INC2 ;&print-tree-indent JSR2
( indent end )
,&print-tree/length-more JMP
&print-tree/length-done
( indent end )
POP2
POP2
RTN
( -- )
&compute
;&read-version JSR2 POP
;&read-type JSR2 ( type )
( type )
DUP #04 EQU ,&compute/is-lit JCN
;&get-bit JSR2 ( type has-count )
,&compute/has-count JCN
,&compute/has-length JMP
&compute/is-lit
( type )
POP
;&push JSR2 ( p* )
;&read-value JSR2
;&peek JSR2 ;print64 JSR2
;&stackdump JSR2 LF
RTN
&compute/has-count
( type )
;&read-count11 JSR2 ( type count )
DUP2
( type count counter )
&compute/count-more
DUP2 #0000 EQU2 ,&compute/count-done JCN
( type count )
;&compute JSR2
( type count )
DEC2
,&compute/count-more JMP
&compute/count-done
( type count 0 )
POP2
( type count )
ROT ( count type )
;&execute JSR2
RTN
&compute/has-length
( type )
#0000 ( type count end )
;&read-length15 JSR2 [ ;&_counter LDA2 ] ADD2
( type count end )
&compute/length-more
( type count end )
DUP2 [ ;&_counter LDA2 ] LEQ2 ,&compute/length-done JCN
( type count end )
;&compute JSR2
( type count end )
SWP2 INC2 SWP2
( type count+1 end )
,&compute/length-more JMP
&compute/length-done
( type count end )
POP2
( type count )
ROT ( count type )
;&execute JSR2
RTN
( count type -- result )
&execute
( count type )
( type count )
;&op-execute JSR2
RTN
&stackdump
P< 20 "| 20 >P
;&_stack ( p* )
&stackdump/loop
( p* )
DUP2 [ ;&_stacktop LDA2 ] GEQ2 ,&stackdump/done JCN
DUP2 ;print64/no-pad JSR2 SP
( p* )
LIT2 CELLSIZE ADD2
,&stackdump/loop JMP
&stackdump/done
( p* )
POP2
RTN
( allocates cell, returns previous top )
( -- cell* )
&push
[ ;&_stacktop LDA2 ]
DUP2 LIT2 CELLSIZE ADD2 [ ;&_stacktop STA2 ]
[ ;&_stacktop LDA2 ] ;&_stackend GEQ2 ,&overflow JCN
( top* )
RTN
&overflow
P< "stack_overflow >P !
( deallocates cell, returns previous top )
( -- cell* )
&pop
[ ;&_stacktop LDA2 ]
LIT2 CELLSIZE SUB2 DUP2 [ ;&_stacktop STA2 ]
[ ;&_stacktop LDA2 ] ;&_stack LTH2 ,&underflow JCN
( p* )
RTN
&underflow
P< "stack_underflow >P !
( returns current top unless stack is empty )
( -- cell* )
&peek
[ ;&_stacktop LDA2 ]
LIT2 CELLSIZE SUB2
DUP2 ;&_stack LTH2 ,&underflow JCN
( p* )
RTN
( count type -- )
&op-execute
( count type )
STHk #00 SWP 2** ;&op-fns ADD2 LDA2
( count fn : type )
STHr
( count fn type )
;&reduce JSR2
RTN
( '+ '* 'm 'M '# '> '< '= )
[ &op-fns :&op-add :&op-mul :&op-min :&op-max :&op-nop :&op-gth :&op-lth :&op-equ ]
( count fn type -- )
&reduce
STH
SWP2 ( fn count : type )
STHr ROT ROT ( fn type count )
DUP2 #0001 EQU2 ,&reduce/single JCN
#0000 SWP2 DEC2 DO
( fn type i )
STH2
( fn type : i )
DUP ;&print-op JSR2
P< 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 >P
ROT ROT STH2
( type : fn i )
;&pop JSR2 ( n1* )
;&pop JSR2 ( n1* n2* )
SWP2 ( n2* n1* )
;&push JSR2 ( n2* n1* out* )
ROT2 ROT2 ( out* n2* n1* )
( type out* n2* n1* : fn i )
STH2rk JSR2
;&stackdump JSR2 LF
( type : fn i )
STH2r
( type fn : i )
ROT
( fn type : i )
STH2r
( fn type i )
LOOP
( fn type )
POP
POP2
RTN
&reduce/single
( fn type 1 )
POP2
P< "# >P
;&print-op JSR2
P< 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 >P
POP2
;&stackdump JSR2 LF
RTN
( out* n2 n1 -- )
&op-nop
POP2 POP2 POP2
RTN
[ &tmp $8 ]
( out* n2* n1* -- )
&op-add
;&tmp SWP2 ;copy64 JSR2
( out* n2* )
;&tmp SWP2 ;add64 JSR2
( out* )
;&tmp ;copy64 JSR2
RTN
( out* n2* n1* -- )
&op-mul
;mul64 JSR2 ( out* )
;mul64/get-lo JSR2
RTN
( out* n2* n1* -- )
&op-equ
;&tmp ROT2 ROT2 ;sub64 JSR2
;&tmp ;is-non-zero64 JSR2
NOT
( out* f )
#00 SWP ;short-to-64 JSR2
RTN
( out* n2* n1* -- )
&op-lth
;&tmp ROT2 ROT2 ;sub64 JSR2
;&tmp ;is-positive64 JSR2
NOT
( out* f )
#00 SWP ;short-to-64 JSR2
RTN
( out* n2* n1* -- )
&op-gth
;&tmp ROT2 ROT2 ;sub64 JSR2
;&tmp ;is-positive64 JSR2
;&tmp ;is-non-zero64 JSR2
AND
( out* f )
#00 SWP ;short-to-64 JSR2
RTN
( out* n2* n1* -- )
&op-min
DUP4 ;&tmp ROT2 ROT2 ;sub64 JSR2
;&tmp ;is-positive64 JSR2
( out* n2* n1* n2>=n1 )
NOT
,&min/n2-greater JCN
( out* n2* n1* )
NIP2
;copy64 JSR2
RTN
&min/n2-greater
( out* n2* n1* )
POP2
;copy64 JSR2
RTN
( out* n2 n1 -- )
&op-max
DUP4 ;&tmp ROT2 ROT2 ;sub64 JSR2
;&tmp ;is-positive64 JSR2
( out* n2* n1* n2>=n1 )
,&max/n2-greater JCN
( out* n2* n1* )
NIP2
;copy64 JSR2
RTN
&max/n2-greater
( out* n2* n1* )
POP2
;copy64 JSR2
RTN