aoc-forth

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

commit eab8c096cfabeae45ee44c9d9268268de4559647
parent 19fe7ce7a884d71a03bec28725ef894921e3dbf1
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  5 Dec 2022 22:26:43 -0800

Day 6

Diffstat:
A2022/06.fs | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/2022/06.fs b/2022/06.fs @@ -0,0 +1,29 @@ +: read-input ( -- a u ) pad pad 4095 accept ; +: nget ( a u -- c1 c2 c3 c4 ) + 0 do dup r@ + c@ swap loop drop ; +: array create allot does> + ; + +128 constant len +len array countbuf + +: 1+c! ( a -- ) dup c@ 1+ swap c! ; +: no-dups ( a -- f ) + len 0 do dup r@ + c@ 1 > + if drop false unloop exit then loop drop true ; + +: ndiff ( c1 c2 c3 c4 n -- f ) + 0 countbuf 128 erase + 0 do countbuf 1+c! loop + 0 countbuf no-dups ; + +variable n +: solve + n @ - 0 do dup r@ + + n @ nget n @ ndiff if r@ n @ + . cr drop unloop exit + then loop ; +: solveboth + read-input 2dup + 4 n ! solve \ part 1 + 14 n ! solve \ part 2 + bye ; +solveboth