aoc-forth

Advent of code solutions in UF forth
git clone git://git.alexwennerberg.com/aoc-forth
Log | Files | Refs | README

commit 03ce5fba39a38c9b6d26eb4118ea7c1061b1f635
parent 84d1b3c3df3638f07004ca2f0d99e9f70eab3054
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 10 Dec 2022 00:07:44 -0800

Add unfinished day 7 and 9

Diffstat:
A2022/07.fs | 47+++++++++++++++++++++++++++++++++++++++++++++++
A2022/09.fs | 30++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/2022/07.fs b/2022/07.fs @@ -0,0 +1,47 @@ +include ../utils/dmath.f +36 constant dollar +11822 constant dotdot + +variable total +\ double stack - 2 cells (4 bytes) +: stack create ( n ) here cell+ , 4 * allot does> ( [a] -- ) ; +16 stack filesystem + +: push ( n a -- ) tuck @ 2! 4 swap +! ; +: pop ( a -- n ) dup -4 swap +! @ 2@ ; +: ?empty ( a -- f ) dup @ 4 - = ; +: clear-stack ( a -- ) dup 4 + swap ! ; + +: 2!0 0 0 rot 2! ; + +variable tmp +: to-double ( a u -- d ) tmp 2!0 +0 do dup r@ + c@ h# 30 - 0 +tmp 2@ 10 1 m*/ d+ tmp 2! loop drop tmp 2@ ; + +\ stack / a +\ small- +\ / = 13737513 + 850456 +\ a = 29 +\ e = 584 +\ cd .. -> check if your value is < 100k, if so, add to total + +10000 constant deadbuf +: clear-line deadbuf 32 accept drop ; +: read-line ( -- a u ) pad pad 40 accept ; +: cd 2 accept clear-line dotdot = if then \ read the cd parameter, if it is dotdot, do the above logic ; +: ls \ read until dollar sign, get value, push to stack +begin +dollar <> while + +repeat +drop +pad 40 accept +pad 40 accept +; +: $ noop ; +: dir pad 40 accept drop ; \ eats input. ignore this line + +include sample.txt + +\ current directorgt diff --git a/2022/09.fs b/2022/09.fs @@ -0,0 +1,30 @@ +99 constant size +: to-num ( a u -- n ) + 0 -rot 0 do dup r@ + c@ h# 30 - rot 10 * + swap loop drop ; + +create visited 1000 allot ; +variable x +variable y \ positive y +variable tailx +variable taily + +\ references head +: below ( -- x y ) x @ y @ 1- ; +: above x @ y @ 1+ ; +: toright x @ 1+ y @ ; +: toleft x @ 1- y @ ; + +: xdiff x @ tailx @ - abs ; +: ydiff y @ taily @ - abs ; +: move-tail ( direction to place -- ) xdiff 1 > ydiff 1 > or if +execute .s cr taily ! tailx ! +else drop then ; + +: get-num bl parse to-num ; +: U get-num 0 do 1 y +! ['] below move-tail loop ; +: D get-num 0 do -1 y +! ['] above move-tail loop ; +: R get-num 0 do 1 x +! ['] toleft move-tail loop ; +: L get-num 0 do -1 x +! ['] toright move-tail loop ; +include sample.txt +x ? y ? +bye