-
Notifications
You must be signed in to change notification settings - Fork 1
/
day18_lib.tal
401 lines (338 loc) · 8.17 KB
/
day18_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
~library/console.lib.tal
~library/string.tal
~library/math.tal
@snailcalc
( get-byte sn* -- )
&parse
[ ;&sn STA2 ]
[ ;&get-byte STA2 ]
#0000 [ ;&value STA2 ]
#00 [ ;&value-started STA ]
&parse/loop
LIT2 [ &get-byte $2 ] JSR2
DUP #0a EQU ;&parse/done JCN2
DUP #00 EQU ;&parse/done JCN2
DUP LIT '[ NEQ ,&parse/not-[ JCN
POP
#8000 ;&parse/append JSR2
;&parse/loop JMP2
&parse/not-[
DUP LIT '] EQU ,&parse/value-done JCN
DUP LIT ', EQU ,&parse/value-done JCN
( has to be a digit )
LIT '0 SUB #00 SWP ( digit-val )
[ ;&value LDA2 ] #000a MUL2 ADD2 ( value+10*digit-val )
[ ;&value STA2 ]
#01 [ ;&value-started STA ]
;&parse/loop JMP2
&parse/value-done
( ]-or-, )
POP
;&parse/append-value JSR2
;&parse/loop JMP2
&parse/done
( terminator-byte )
POP
;&parse/append-value JSR2
#ffff [ ;&sn LDA2 ] STA2
RTN
[ &sn $2 &value $2 &value-started $1 ]
( word -- )
&parse/append
[ ;&sn LDA2 ] STA2
[ ;&sn *2++ ]
RTN
( -- )
&parse/append-value
[ ;&value-started LDA ] NOT ,&parse/not-started JCN
[ ;&value LDA2 ] ;&parse/append JSR2
#0000 [ ;&value STA2 ]
#00 [ ;&value-started STA ]
&parse/not-started
RTN
( sn* -- sn* word )
&load-node
( sn* )
LDA2k SWP2 2++ SWP2
( sn+2* value )
( SP LIT '( EMIT OVR2 DEC2 DEC2 DBGSHORTn POP2 LIT ': EMIT DBGSHORTn LIT ') EMIT SP )
( sn* word )
DUP2 #ffff EQU2 ;&end-of-tree JCN2
RTN
( sn* -- )
&print
;&print-rec JSR2
POP2
RTN
( sn* -- sn* )
&print-rec
;&load-node JSR2
( sn* word )
DUP2 #8000 NEQ2 ,&print/not-[ JCN
POP2
LIT '[ EMIT
( sn* )
;&print-rec JSR2
LIT ', EMIT
;&print-rec JSR2
( sn* )
LIT '] EMIT
RTN
&print/not-[
;print-short-decimal JSR2
RTN
&end-of-tree
P< "unexpected_end_of_tree >P
!
RTN
( sn* -- exploded )
&explode
#0000 [ ;&explode/last-leaf-ptr STA2 ]
#00 ( sn* depth )
;&explode-rec JSR2
( sn* depth exploded )
NIP NIP NIP
( exploded )
RTN
( sn* depth -- sn* depth exploded )
&explode-rec
STH
( sn* : depth )
;&load-node JSR2
( sn* word : depth )
DUP2 #8000 NEQ2 ;&explode/not-[ JCN2
POP2
( sn* : depth )
( sn* : depth )
STHr
( DBGBYTEn SP )
( sn* depth )
DUP #04 LTH ,&explode/not-deep JCN
( LIT '! EMIT )
( sn* depth )
STH ( sn* : depth )
( found a node that needs exploding )
( propagate left value )
DUP2 LDA2 ( sn* value )
( patch the last leaf encountered )
[ ;&explode/last-leaf-ptr LDA2 ] #0000 EQU2 ,&explode/no-last-leaf JCN
( sn* value )
[ ;&explode/last-leaf-ptr LDA2 ] LDA2 ADD2 ( sn* value+last-leaf )
DUP2 [ ;&explode/last-leaf-ptr LDA2 ] STA2
&explode/no-last-leaf
( sn* value )
POP2
( sn* )
( propagate right value )
DUP2 2++ ;&propagate-right JSR2
( replace it with a zero literal )
DUP2 #0000 SWP2 2-- STA2
( shift remaining left )
( sn* : depth )
DUP2 ;&shift-left4 JSR2
( signal explosion, unwind )
STHr ( sn* depth )
#01
RTN
&explode/not-deep
INC ( sn* depth+1 )
;&explode-rec JSR2
( sn* depth+1 exploded )
,&explode-exploded JCN
( sn* depth+1 )
;&explode-rec JSR2
( sn* depth+1 exploded )
,&explode-exploded JCN
DEC ( sn* depth )
#00
RTN
&explode/not-[
( sn* word : depth )
POP2
( sn* : depth )
( remember the last leaf we saw )
DUP2 2-- [ ,&explode/last-leaf-ptr STR2 ]
( sn* : depth )
STHr ( sn* depth )
#00
RTN
[ &explode/last-leaf-ptr $2 ]
&explode-exploded
( sn* depth )
( LIT '* EMIT )
#01
RTN
( sn* -- )
&shift-left4
( sn* )
DUP2 4++ ( sn* sn+4* )
;© JMP2
( sn* -- )
&propagate-right
LDA2k [ ;&propagate-right/value STA2 ]
2++ ( sn+2* )
&propagate-right/loop
( sn* )
LDA2k ( sn* word )
DUP2 #ffff EQU2 ;&propagate-right/done JCN2
DUP2 #8000 LTH2 ,&propagate-right/found JCN
POP2
2++
,&propagate-right/loop JMP
&propagate-right/done
( sn* ffff )
POP2 POP2
RTN
[ &propagate-right/value $2 ]
&propagate-right/found
( sn* word )
[ ,&propagate-right/value LDR2 ] ADD2
( sn* word+value )
SWP2 STA2
RTN
( sn* -- didsplit )
&split
;&split-rec JSR2
NIP NIP
RTN
( sn* -- sn* didsplit )
&split-rec
;&load-node JSR2
( sn* word )
DUP2 #8000 NEQ2 ,&split/not-[ JCN
POP2
( sn* )
;&split-rec JSR2
,&split/splitted JCN
;&split-rec JSR2
,&split/splitted JCN
( sn* )
#00
RTN
&split/not-[
( sn* value )
( DBGSHORTDEC )
DUP2 #000a LTH2 ,&split/not-big JCN
( sn* value )
( make room for two more nodes )
OVR2 ;&shift-right4 JSR2
( create parent node )
OVR2 2-- #8000 SWP2 STA2
( write left leaf )
OVR2 OVR2 #01 SFT2 SWP2 STA2
( write right leaf )
OVR2 2++ OVR2 INC2 #01 SFT2 SWP2 STA2
( signal split, unwind )
( sn* value )
POP2
,&split/splitted JMP
&split/not-big
( sn* value )
POP2
#00
RTN
&split/splitted
#01
RTN
( sn* -- )
&shift-right4
( sn* )
( read and replace first two words with zeros )
LDA2k STH2 DUP2 #0000 SWP2 STA2 2++ ( sn*+2 : word1 )
LDA2k STH2 DUP2 #0000 SWP2 STA2 2++ ( sn*+4 : word2 word1 )
SWP2r ( sn*+4 : word1 word2 )
&shift-right4/loop
( read next word, overwrite it with word1 )
LDA2k STH2 ( sn*+4 : word3 word1 word2 )
SWP2r ( sn*+4 : word1 word3 word2 )
STH2rk OVR2 STA2 ( sn*+4 : word1 word3 word2 )
( did we just write ffff? )
STH2r #ffff EQU2 ,&shift-right4/done JCN
SWP2r ( sn*+4 : word2 word3 )
2++ ( sn*+6 : word2 word3 )
,&shift-right4/loop JMP
&shift-right4/done
( sn*+x : word word )
POP2r POP2r
POP2
RTN
( sn* -- )
&reduce
DUP2 ;&explode JSR2 ,&reduce JCN
DUP2 ;&split JSR2 ,&reduce JCN
POP2
RTN
( sndest* snsrc* -- )
©
SWP2 STH2 ( snsrc* : sndest* )
©/loop
LDA2k ( snsrc* word : sndest* )
DUP2 STH2rk STA2 ( snsrc* word : sndest* )
#ffff EQU2 ,©/done JCN
2++ 2++r ( snsrc+2* : sndest+2* )
,©/loop JMP
©/done
( snsrc+x* : sndest+x* )
POP2 POP2r
RTN
( sn1* sn2* -- )
&add
STH2 ( sn1* : sn2* )
( make room )
( sn1* : sn2* )
DUP2 ;&shift-right4 JSR2
( a bit less room, please )
( sn1* : sn2* )
DUP2 DUP2 2++ ( sn1* sn1* sn1+2* : sn2* )
;© JSR2
( add root )
#8000 OVR2 STA2
( find end )
&add/loop
( sn1* )
2++ LDA2k #ffff NEQ2 ,&add/loop JCN
( sn1end* : sn2* )
STH2r ;© JSR2
RTN
( get-byte result* -- )
&list-add
[ ;&list-add/result STA2 ]
[ ;&list-add/get-byte STA2 ]
( load first number )
[ ;&list-add/get-byte LDA2 ] [ ;&list-add/result LDA2 ] ;&parse JSR2
&list-add/loop
( load next number )
[ ;&list-add/get-byte LDA2 ] ;&list-add/tmp ;&parse JSR2
( is it the end? )
[ ;&list-add/tmp LDA2 ] #ffff EQU2 ,&list-add/done JCN
( add up into result )
[ ;&list-add/result LDA2 ] ;&list-add/tmp ;&add JSR2
( reduce result )
[ ;&list-add/result LDA2 ] ;&reduce JSR2
,&list-add/loop JMP
&list-add/done
RTN
[ &list-add/get-byte $2 &list-add/result $2 &list-add/tmp $200 ]
( sn* -- mag )
&magnitude
( sn* )
;&magnitude-rec JSR2
( sn* mag )
NIP2
RTN
( sn* -- sn* )
&magnitude-rec
;&load-node JSR2
( sn* word )
DUP2 #8000 NEQ2 ,&magnitude/not-[ JCN
POP2
( sn* )
;&magnitude-rec JSR2
#0003 MUL2 STH2 ( sn* : 3*leftmag )
;&magnitude-rec JSR2
#0002 MUL2 ( sn* 2*rightmag : 3*leftmag )
STH2r ADD2 ( sn* 2*rightmag+3*leftmag )
RTN
&magnitude/not-[
( sn* value )
RTN