-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday13_lib.tal
390 lines (335 loc) · 6.7 KB
/
day13_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
( get-byte -- x-or-ffff y-or-ffff )
@parse-dot-line
[ ;&get-byte-addr STA2 ]
#0000 ( x )
&parse-x-char
;&get-byte JSR2 ( b )
DUP #0a NEQ ,¬-empty JCN
POP POP2 #ffff #ffff RTN
¬-empty
DUP #30 LTH ,&x-done JCN
#30 SUB #00 SWP STH2
#000a MUL2 STH2r ADD2
,&parse-x-char JMP
&x-done
( ', )
POP
#0000 ( x y )
&parse-y-char
;&get-byte JSR2 ( b )
DUP #30 LTH ,&done JCN
#30 SUB #00 SWP STH2
#000a MUL2 STH2r ADD2
,&parse-y-char JMP
&done
( x y 0a )
POP
( x y )
RTN
&get-byte
LIT2 [ &get-byte-addr $2 ] JMP2
( get-byte -- num-or-ffff 'x-or-'y-or-ff )
@parse-fold-line
[ ;&get-byte-addr STA2 ]
&skip-to-axis
;&get-byte JSR2 ( b )
DUP #00 NEQ ,¬-empty JCN
POP #ffff #ff RTN
¬-empty
DUP LIT 'x EQU ,&axis-found JCN
DUP LIT 'y EQU ,&axis-found JCN
POP
,&skip-to-axis JMP
&axis-found
( 'x-or-'y )
( eat '= )
;&get-byte JSR2 POP
STH
#0000 ( n )
&parse-digit
;&get-byte JSR2 ( b )
DUP #30 LTH ,&done JCN
#30 SUB #00 SWP STH2
#000a MUL2 STH2r ADD2
,&parse-digit JMP
&done
POP
STHr
RTN
&get-byte
LIT2 [ &get-byte-addr $2 ] JMP2
( get-byte -- )
@parse-lines
[ ;&get-byte-addr STA2 ]
#ffff ;dots STA2
#ffff ;folds STA2
;dots ( dots* )
&next-dot
[ ;&get-byte-addr LDA2 ] ;parse-dot-line JSR2
DUP2 #ffff EQU2 ,&dots-done JCN
STH2 OVR2 STA2 STH2r
OVR2 2++ STA2
4++
,&next-dot JMP
&dots-done
( dots* ffff ffff )
POP2 POP2
#ffff SWP2 STA2
;folds ( folds* )
&next-fold
[ ;&get-byte-addr LDA2 ] ;parse-fold-line JSR2
DUP2 #ffff EQU2 ,&folds-done JCN
( folds* num xy )
STH OVR2 STA2
DUP2 2++ STHr ROT ROT STA
#0003 ADD2
,&next-fold JMP
&folds-done
( folds* ffff ff )
POP POP2
#ffff SWP2 STA2
RTN
[ &get-byte-addr $2 ]
( -- )
@dump-dots
;dots
#0000 STH2
( dots* : cnt )
&next
LDA2k ( dots* x )
DUP2 #ffff EQU2 ,&done JCN
DBGSHORTDECn LIT ', EMIT POP2
DUP2 2++ LDA2 DBGSHORTDECn SP POP2
INC2r
4++
,&next JMP
&done
( dots* ffff )
POP2 POP2
P< "n= >P
STH2r DBGSHORTDECn POP2
LF
RTN
( -- )
@dump-folds
;folds
( folds* )
&next
LDA2k ( folds* x )
DUP2 #ffff EQU2 ,&done JCN
STH2
DUP2 2++ LDA EMIT
LIT '= EMIT
STH2r
DBGSHORTDECn POP2
SP
#0003 ADD2
,&next JMP
&done
( folds* ffff )
POP2 POP2
LF
RTN
@save-dots
( src* dest* cnt -- )
;dots ;dots2 #1000 ;memcpy JSR2
RTN
@save-dots2
( src* dest* cnt -- )
;dots ;dots3 #1000 ;memcpy JSR2
RTN
@restore-dots
( src* dest* cnt -- )
;dots2 ;dots #1000 ;memcpy JSR2
RTN
( fold* -- )
@process-fold
LDA2k [ ;&fold-val STA2 ]
2++ LDA [ ;&fold-axis STA ]
;dots
( dots* )
&next
LDA2k ( dots* x )
#ffff EQU2 ,&done JCN
( dots* )
LIT2 [ &fold-val $2 ]
( dots* fold-val )
LIT [ &fold-axis $1 ]
( dots* fold-val fold-axis )
LIT 'x EQU
( dots* fold-val fold-axis=x? )
,&axis-is-x JCN
( dots* fold-val )
OVR2 2++
( dots* fold-val yptr )
;&do-fold JSR2
( dots* )
,&continue JMP
&axis-is-x
( dots* fold-val )
OVR2
( dots* fold-val xptr )
;&do-fold JSR2
( dots* )
&continue
4++
,&next JMP
&done
( dots* )
POP2
RTN
( fold-val yptr -- )
&do-fold
STH2k ( fold-val yptr : yptr )
( fold-val yptr : yptr )
LDA2 ( fold-val y : yptr )
SWP2
( y fold-val : yptr )
LTH2k ,&inside JCN
( y fold-val : yptr )
SUB2k
( y fold-val distance : yptr )
[ ;fold-scale LDA2 ]
MUL2 20//
( y fold-val scale*distance : yptr )
[ ;fold-inside LDA ] NOT ,&stay-outside JCN
SUB2
,&fold-done JMP
&stay-outside
( y fold-val scale*distance : yptr )
ADD2
&fold-done
( y (fold-val)±distance : yptr )
NIP2
( newy : yptr )
STH2r STA2
RTN
&inside
( y fold-val : yptr )
POP2r POP2 POP2
RTN
( -- )
@distinct-dots
;dots STH2 ( : dest-dots* )
;dots ( src-dots* : dest-dots* )
&next
LDA2k ( src-dots* x : dest-dots* )
DUP2 #ffff EQU2 ;&done JCN2
( src-dots* x : dest-dots* )
[ ;&x STA2 ]
( src-dots* : dest-dots* )
DUP2 2++ LDA2 [ ;&y STA2 ]
( scan right to find a duplicate )
( P< "scanning 20 >P DBGSHORT )
( src-dots* : dest-dots* )
DUP2 4++
( src-dots* dots* : dest-dots* )
&scan-next
( P< 20 "vs 20 >P DBGSHORT )
LDA2k ( dots* x )
DUP2 #ffff EQU2 ,&scan-done JCN
( dots* x )
( DBGSHORTDECn LIT '= EMIT [ ;&x LDA2 ] DBGSHORTDECn POP2 LIT '? EMIT LF )
[ ;&x LDA2 ] NEQ2 ,&scan-continue JCN
( dots* )
DUP2 2++ LDA2 ( dots* y )
[ ;&y LDA2 ] NEQ2 ,&scan-continue JCN
( P< "dup 0a >P )
( duplicate found )
#0000 ,&scan-done JMP
&scan-continue
( dots* )
4++ ,&scan-next JMP
&scan-done
( dots* 0000-or-ffff )
NIP2 #ffff NEQ2
( has-duplicate? )
,¬-unique JCN
( copy to output list )
( src-dots* : dest-dots* )
DUP2 LDA2 STH2rk STA2
DUP2 2++ LDA2 STH2rk 2++ STA2
STH2r 4++ STH2
( DBGSHORTn SP LIT ': EMIT SP STH2rk DBGSHORT POP2 )
¬-unique
4++
;&next JMP2
&done
( src-dots* ffff : dest-dots* )
POP2 POP2
#ffff STH2r STA2
RTN
[ &x $2 &y $2 ]
( -- )
@process-folds
;folds
( folds* )
&next
LDA2k ( folds* x )
DUP2 #ffff EQU2 ,&done JCN
( folds* x )
POP2
( folds* )
DUP2 ;process-fold JSR2
( folds* )
;distinct-dots JSR2
[ ;post-fold-hook LDA2 ] JSR2
#0003 ADD2
,&next JMP
&done
( folds* ffff )
POP2 POP2
RTN
@post-fold-nop
RTN
@post-fold-hook :post-fold-nop
@draw-dots
P< "--- >P LF
[ ;dots-w LDA2 ]
[ ;dots-h LDA2 ] DUP2 #0000 GTH2 ,&size-ok JCN
LIT '! EMIT LF
POP2 POP2 RTN
&size-ok
SWP2 STH2 STH2 ( : h w )
#0000 STH2 ( : row h w )
&next-row
STH2r STH2r LIT 20 [ ;dots-w LDA2 ] ;row ;memset JSR2 STH2 STH2
;dots
( dots* : row )
&next
LDA2k ( dots* x )
DUP2 #ffff EQU2 ,&done JCN
OVR2 2++ LDA2 STH2rk EQU2 ,&row-match JCN
POP2 ,&continue JMP
&row-match
;row ADD2
( dots* row+x )
STH2 LIT '# STH2r STA
&continue
4++
,&next JMP
&done
( dots* ffff : row h w )
POP2 POP2
STH2r STH2r
LIT 0a ;row STH2rk ADD2 STA
LIT 00 ;row STH2rk ADD2 INC2 STA
STH2 STH2
;row ;print JSR2
INC2r STH2rk POP2
GTH2rk STHr ;&next-row JCN2
POP2r
POP2r POP2r
RTN
~library/console.lib.tal
~library/string.tal
~library/math.tal
|7000
@dots $1000
@dots-w $2 @dots-h $2
@fold-scale 0020
@fold-inside 01
@dots2 $1000
@dots3 $1000
@folds $100
@row $1000