-
Notifications
You must be signed in to change notification settings - Fork 1
/
day18.tal
89 lines (68 loc) · 1.54 KB
/
day18.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
~library/macros.tal
~library/devices.tal
( variables )
|0000
( init )
|0100 @program
;on-arg-char .Console/vector DEO2
BRK !
@on-arg-char
.Console/read DEI ( char )
DUP #0a EQU ,&complete JCN
;argv #00 ,&argptr LDR ADD2 STA
,&argptr LDR INC ,&argptr STR
BRK !
&complete
POP
;arg-received JMP2
&argptr $1
@argv $20
%COUNT { #0064 }
( arg is in @argv )
@arg-received
LIT ': EMIT
;argv ;print JSR2
LF
#0000 [ ;&best-mag STA2 ]
#0000 COUNT DO
#0000 COUNT DO
( i j )
OVR2 DBGSHORTDECn SP POP2 DBGSHORTDEC
OVR2 ;&sn1 ;read-nth JSR2
DUP2 ;&sn2 ;read-nth JSR2
;&sn1 ;snailcalc/print JSR2 LF
;&sn2 ;snailcalc/print JSR2 LF
;&sn1 ;&sn2 ;snailcalc/add JSR2
;&sn1 ;snailcalc/reduce JSR2
P< "mag= >P ;&sn1 ;snailcalc/magnitude JSR2 DUP2 ;print-short-decimal JSR2
DUP2 [ ;&best-mag LDA2 ] LTH2 ,¬-new-best JCN
LIT '! EMIT
DUP2 [ ;&best-mag STA2 ]
¬-new-best
LF
POP2
LOOP
LOOP
P< "best-mag= >P [ ;&best-mag LDA2 ] ;print-short-decimal JSR2 LF
BRK
[ &sn1 $500 &sn2 $500 &best-mag $2 ]
( n sn* -- )
@read-nth
[ ;&snp STA2 ]
;open-file JSR2
#0000 SWP2 INC2 DO
;read-byte [ ;&snp LDA2 ] ;snailcalc/parse JSR2
LOOP
RTN
[ &snp $2 ]
@open-file
;argv .File/name DEO2
#0001 .File/length DEO2
RTN
( -- next-byte )
@read-byte
#00 ;&byte STA ( clear byte so that EOF reads as zero )
;&byte .File/read DEO2
LIT &byte 00 ( <- this byte is patched by the two previous instructions! )
RTN
~day18_lib.tal