commit 2dc79ef970166b4204b705da7d7e5460b4ef4129
parent 11134aa4de8a6433811789e805ced603bb9cee2f
Author: alex wennerberg <alex@alexwennerberg.com>
Date: Thu, 1 Dec 2022 11:58:59 -0800
solution but missing the double in the input
Diffstat:
3 files changed, 33 insertions(+), 36 deletions(-)
diff --git a/2022/01.fs b/2022/01.fs
@@ -0,0 +1,32 @@
+include ../utils/dmath.f
+
+: darray
+create 4 * allot does> swap 4 * + ;
+
+6 darray foo
+variable cals
+
+: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ;
+
+\ low to high
+: get-nums ( -- d3 d2 d1 ) 0 foo 2@ 1 foo 2@ 2 foo 2@ ;
+: reset-cals 0 0 cals 2! ;
+: 4dup 3 pick 3 pick 3 pick 3 pick ;
+
+\ stole from peter
+: dpivot ( d1 d2 -- min max ) 4dup d< if else 2swap then ;
+: check-total
+cals 2@ d. cr
+get-nums cals 2@
+dpivot 2 foo 2!
+dpivot 1 foo 2!
+dpivot 0 foo 2!
+2drop get-nums d. d. d. cr ;
+
+: add-calories ( u -- ) 0 \ double conversion
+ cals 2@ d+ cals 2! ;
+: process-line ( -- a n ) pad pad 10 accept dup 0=
+ if 2drop check-total reset-cals exit
+ then to-num add-calories ;
+: solve reset-cals 3000 0 do process-line loop ;
+solve
diff --git a/2022/01.py b/2022/01.py
@@ -17,4 +17,4 @@ for line in sys.stdin:
alllines.sort()
alllines.reverse()
-print(alllines[0] + alllines[1] + alllines[2])
+print(alllines[0], alllines[1], alllines[2])
diff --git a/2022/01a.fs b/2022/01a.fs
@@ -1,35 +0,0 @@
-\ failure
-
-include ../utils/dmath.f
-
-variable cals
-variable top
-variable second
-variable third
-
-\ hell
-: push-var
-cals 2@
-2dup 2dup 2dup
-top 2@ swap d< if
-second 2@ third 2! top 2@ second 2! 2dup top 2!
-then 2drop 2drop 2drop exit
-second 2@ swap d< if
-second 2@ third 2! 2dup second 2!
-then 2drop 2drop exit
-third 2@ swap d< if
-2dup third 2!
-then 2drop exit
-;
-
-: to-num ( a u -- n ) 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ;
-: read-line ( -- a n ) pad pad 10 accept ;
-: add-calories to-num 0 cals 2@ d+ cals 2! ;
-: group-done? ( -- f ) dup 0= if push-var
-top 2@ second 2@ third 2@ d. d. d. cr
-1 exit then 0 ;
-: solve
- 2250 0 do read-line group-done? if read-line then add-calories
- loop
-;
-solve