-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday17_lib.tal
150 lines (131 loc) · 3.57 KB
/
day17_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
~library/console.lib.tal
~library/string.tal
~library/math.tal
@probe
[ &xmin $2 &xmax $2
&ymin $2 &ymax $2
&dx $2 &dy $2
&x $2 &y $2 &vx $2 &vy $2
&yreached $2
&quiet 00 ]
( xmin xmax ymin ymax )
&set-target
[ ;&ymax STA2 ]
[ ;&ymin STA2 ]
[ ;&xmax STA2 ]
[ ;&xmin STA2 ]
RTN
( dx dy -- hit )
&launch
[ ;&dy STA2 ]
[ ;&dx STA2 ]
[ ;&dx LDA2 ] [ ;&vx STA2 ]
[ ;&dy LDA2 ] [ ;&vy STA2 ]
#0000 [ ;&x STA2 ]
#0000 [ ;&y STA2 ]
#0000 [ ;&yreached STA2 ]
#0000 #ffff DO
;&print JSR2
;&check JSR2 ;&continue JCN2
,&hit JCN
[ ;&quiet LDA ] ,&launch/no-missed-msg JCN
P< "missed >P LF
&launch/no-missed-msg
UNLOOP #00 RTN
&hit
P< "hit 20 "dx= >P [ ;&dx LDA2 ] ;print-short-decimal/signed JSR2 SP
P< "dy= >P [ ;&dy LDA2 ] ;print-short-decimal/signed JSR2 SP
P< "x= >P [ ;&x LDA2 ] ;print-short-decimal/signed JSR2 SP
P< "y= >P [ ;&y LDA2 ] ;print-short-decimal/signed JSR2 SP
P< "yreached= >P [ ;&yreached LDA2 ] ;print-short-decimal/signed JSR2 LF
UNLOOP #01 RTN
&continue
POP
;&step JSR2
LOOP
#00
RTN
( -- )
&print
[ ;&quiet LDA ] ,&print/skip JCN
[ ;&x LDA2 ] ;print-short-decimal/signed JSR2 SP
[ ;&y LDA2 ] ;print-short-decimal/signed JSR2 LF
&print/skip
RTN
( a b -- a+b or halt )
&add-with-overflow-check
( if inputs have different signs, we're always OK )
EOR2k POP #80 AND ,&no-overflow JCN
( add, then the result must have the same sign as the inputs )
ADD2k OVR2 EOR2 POP #80 AND ,&overflow JCN
&no-overflow
ADD2
RTN
&overflow
P< "overflow! >P
SWP2 ;print-short-decimal/signed JSR2 LIT '+ EMIT SWP2 ;print-short-decimal/signed JSR2 LF
!
( -- )
&step
[ ;&x LDA2 ] [ ;&vx LDA2 ] ;&add-with-overflow-check JSR2 [ ;&x STA2 ]
[ ;&y LDA2 ] [ ;&vy LDA2 ] ;&add-with-overflow-check JSR2 [ ;&y STA2 ]
[ ;&yreached LDA2 ] [ ;&y LDA2 ] SGTH2 ,&step/no-new-height JCN
[ ;&y LDA2 ] [ ;&yreached STA2 ]
&step/no-new-height
[ ;&vx LDA2 ] #0000 EQU2 ,&step/vx0 JCN
[ ;&vx LDA2 ] DEC2 [ ;&vx STA2 ]
&step/vx0
[ ;&vy LDA2 ] DEC2 [ ;&vy STA2 ]
RTN
( -- hit? continue? )
&check
( are we below the target area? )
[ ;&ymin LDA2 ] [ ;&y LDA2 ] SGTH2
,&check/below JCN
( are we right of the target area? )
[ ;&x LDA2 ] [ ;&xmax LDA2 ] SGTH2
,&check/right JCN
( are we above the target area? )
[ ;&y LDA2 ] [ ;&ymax LDA2 ] SGTH2
,&check/above JCN
( are we left of the target area? )
[ ;&xmin LDA2 ] [ ;&x LDA2 ] SGTH2
,&check/left JCN
( we're inside )
( hit,stop )
#01 #00
RTN
&check/below
&check/right
( missed,stop )
#00 #00
RTN
&check/above
&check/left
( notyet,continue )
#00 #01
RTN
( dxmin dxmax dymin dymax )
&scan
#01 [ ;&quiet STA ]
#0000 [ ;&hitcount STA2 ]
INC2 DO
( dxmin dxmax dy )
P< "dy= >P DUP2 ;print-short-decimal/signed JSR2 LF
STH2 ( dxmin dxmax : dy )
DUP4 STH2r ( dxmin dxmax dxmin dxmax dy )
ROT2 ROT2 ( dxmin dxmax dy dxmin dxmax )
INC2 DO
( dxmin dmxax dy dx )
DUP4 SWP2 ( dxmin dxmax dy dx dx dy )
( OVR2 DBGSHORTDECn SP POP2 DBGSHORTDECn SP )
;&launch JSR2
#00 SWP [ ;&hitcount LDA2 ] ADD2 [ ;&hitcount STA2 ]
( dxmin dmxax dy dx )
LOOP
LOOP
( dxmin dxmax )
POP2 POP2
P< "hitcount= >P [ ;&hitcount LDA2 ] DBGSHORTDEC POP2
RTN
[ &hitcount $2 ]