~aleteoryx/up-for-it

up-for-it/aoc/2025/02.1.tal -rw-r--r-- 3.3 KiB
195fe6fdAleteoryx notes 2 days ago
                                                                                
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
( advent of code 2025, day 2, first star )

%dbg { #010e DEO }


|00
@Buffers/num $20 &endnum $1
@Buffers/end $20 &endend $1

$1 @Buffers/in $20 &endin $1

@Buffers/sum $1f &endsum $1


|100
@on-reset ( -> )
	banner
	first-star
	BRK

@banner ( -- )
	;/str !puts
@banner/str
	"advent 20 "of 20 "code 20 "2025, 20 "day 20 "2, 20 "first 20 "star 0a 00


( first star )

@first-star ( -- )
	[ LIT "0 ] ;Buffers/endsum STA
	Input/read
	;Buffers/sum findstr puts endl
	JMP2r

@first-star/on-input ( b -- )
	( DUP #18 DEO )

	DUP [ LIT "- ] NEQ ?{ POP !/dash }
	DUP [ LIT ", ] NEQ ?{ POP !/comma }
	DUP       #0a  NEQ ?{ POP !/comma }

	!Buffers/putin

@first-star/dash ( -- )
	[ LIT2 -Buffers/endnum -Buffers/num ] clearmem
	.Buffers/endnum Buffers/cpyin
	Buffers/reset
	JMP2r

@first-star/comma ( -- )
	[ LIT2 -Buffers/endend -Buffers/end ] clearmem
	.Buffers/endend Buffers/cpyin
	Buffers/reset

	&loop
		Buffers/maybeinc
		;Buffers/endnum incs
		;Buffers/endnum ;Buffers/endend strrcmp ?/loop
	
	Buffers/maybeinc

	JMP2r


( stdlib )

@endl ( n -- )
	#0a18 DEO JMP2r

@puts ( str* -- )
	LDAk #18 DEO
	INC2 LDAk ?puts

	POP2 JMP2r

@putdec2 ( n* -- )
	[ LITr 00 ]

	&loop
		#000a DIV2k STH2k
		MUL2 SUB2
		[ LIT "0 ] ADD NIP
		STH2r ROT STH
		ORAk ?/loop
	POP2
	
	&loop2
		STHr #18 DEO
		STHrk ?/loop2
	POPr JMP2r

@incs ( n* -- )
	LDAk ?{
		[ LIT "1 ]
		ROT ROT STA
		JMP2r
	}
	
	LDAk
	[ LIT "9 ] NEQ ?{
		DUP2
		[ LIT "0 ]
		ROT ROT STA
		&subone
		#0001 SUB2 !incs
	}
	LDAk INC
	ROT ROT STA
	JMP2r

@sumstrs ( a* b* -- )			( takes pointers to the last digits of each number )
	LDAk STH SWP2 LDAk STHr		( b* a* ac bc )
	DUP ?{ POP2 POP2 POP2 JMP2r }	( we've reached the end of b )
	OVR ?{ NIP [ LIT "0 ] }		( if we're at the end of a, pad with 0s )
	[ LIT2 "00 ] SUB2 ADD		( b* a* a+b )
	DUP #0a LTH ?{			( overflow )
		#0a SUB STH
		DUP2 incs/subone	( carry )
		STHr
	}
	[ LIT "0 ] ADD			( convert back to ascii )
	ROTk ROT STA POP		( save digit )
	#0001 SUB2
	SWP2 #0001 SUB2
	!sumstrs			( loop! )

@findend ( p* -- p* )
	INC2 LDAk ?findend
	JMP2r
@findstr ( p* -- p* )
	INC2 LDAk #00 EQU ?findstr
	JMP2r
@strlen ( p* -- len* )
	DUP2 findend SWP2 SUB2 JMP2r

@strrcmp ( a* b* -- neq? )
	INC2 STH2 INC2
	&loop
		[ LIT2r 0001 ] SUB2r
		#0001 SUB2
		LDAk LDArk STHr
		EQUk ?{ POP2r POP2 POP2 #01 JMP2r }
		ORA ?/loop
	POP2r POP2 #00 JMP2r

@clearmem ( end start -- )
	#00 OVR STZ
	INC GTHk ?clearmem
	POP2 JMP2r


( object lib )

@Input/filename "02.input 00
@Input/read ( -- )
	#01ab DEO
	;/filename #a8 DEO2
	
	&loop
		;/byte #ac DEO2
		#a3 DEI #00 EQU ?/endloop	( EOF )
		[ LIT &byte $1 ]
		first-star/on-input
		!/loop

	&endloop
	POP2 JMP2r


( problem code )

@Buffers/putin ( b -- )
	[ LIT2 00 &ptr -Buffers/in ] STZ2k
	INC ,/ptr STR
	POP2 JMP2r
@Buffers/reset ( -- )
	;Buffers/in STZk ,/ptr STR
	POP JMP2r

@Buffers/cpyin ( dst -- )
	[ LIT2 ff _/ptr ] LDR ADD SWP
	&cpyloop				( src dst )
		OVR LDZ OVR STZ
		#0101 SUB2
		OVR LDZ ?/cpyloop
	SWP LDZ SWP STZ				( save the NUL )
	JMP2r

@Buffers/maybeinc ( -- )
	;/num findstr checkstr ?{
		;/endsum ;/endnum sumstrs
	}
	JMP2r


@checkstr ( s* -- ok? )
	DUP2 strlen
	DUP #01 AND #01 NEQ ?{			( odd length, ok by default )
		POP2 POP2 #01 JMP2r
	}
	
	#01 SFT2 ADD2k NIP2 STH2		( s* `s+1/2* )

	&loop
		LDArk LDAk STHr EQU ?{
			POP2r POP2 #01 JMP2r	( OK )
		}
		INC2 INC2r LDArk STHr ?/loop
	
	POP2r POP2 #00 JMP2r			( FAIL )