@@ 4,10 4,10 @@
|00
-@Buffers/num $f &endnum $1
-@Buffers/end $f &endend $1
+@Buffers/num $20 &endnum $1
+@Buffers/end $20 &endend $1
-$1 @Buffers/read $f &endread $1
+$1 @Buffers/in $20 &endin $1
@Buffers/sum $1f &endsum $1
@@ 26,18 26,39 @@ $1 @Buffers/read $f &endread $1
( first star )
-@astr $1 "89 &end "5 $1
-@bstr $1 "10 &end "9 $1
-
@first-star ( -- )
- ;astr/end ;bstr/end sumstrs
-
- ;astr puts endl
-
- ( Input/read )
+ [ 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
@@ 87,12 108,8 @@ $1 @Buffers/read $f &endread $1
ROT ROT STA
JMP2r
-@findend ( p* -- p* )
- INC2 LDAk ?findend
- JMP2r
-
@sumstrs ( a* b* -- ) ( takes pointers to the last digits of each number )
- LDAk STH SWP2 LDAk STHr ( b* a* ac bc ) dbg
+ 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 )
@@ 107,10 124,34 @@ $1 @Buffers/read $f &endread $1
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.test 00
+@Input/filename "02.input 00
@Input/read ( -- )
#01ab DEO
;/filename #a8 DEO2
@@ 125,19 166,46 @@ $1 @Buffers/read $f &endread $1
&endloop
POP2 JMP2r
-
-@Number/one-byte ( b -- )
- #00 SWP
- LIT "0 SUB
- [ LIT2 &val 0000 ]
- #000a MUL2 ADD2
- ,/val STR2
+
+( 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
-@Number/get ( -- n* )
- ,/val LDR2 JMP2r
-@Number/reset ( -- )
- #0000 ,/val STR2 JMP2r
+@Buffers/maybeinc ( -- )
+ ;/num findstr checkstr ?{
+ ;/endsum ;/endnum sumstrs
+ }
+ JMP2r
-( problem code )
+
+@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 )