aoc-forth

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

commit f923cb70fd28e64f18698f3c3200d419afe8240c
parent 5a7c4c8a2ca73d0307b4b661297ad075453e9f7e
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  3 Dec 2022 18:44:11 -0800

Consolidate part 3

Diffstat:
M2022/03.fs | 54++++++++++++++++++++++++++++++++++++++----------------
D2022/03b.fs | 34----------------------------------
2 files changed, 38 insertions(+), 50 deletions(-)

diff --git a/2022/03.fs b/2022/03.fs @@ -1,27 +1,49 @@ -65 constant asciiA -variable total +variable total1 +variable total2 : barray create allot does> + ; -: charray 128 barray ; \ range incl a-z 6 symbols A-Z : c@+ dup c@ >r 1+ r> ; -charray charbuf +128 barray charbuf +128 barray charbuf2 : clear-buf 128 0 do r@ 0 swap charbuf c! loop ; +: clear-buf2 128 0 do r@ 0 swap charbuf2 c! loop ; +: set-charbuf ( a u -- ) 0 do c@+ charbuf 1 swap c! loop drop ; +: set-charbuf2 ( a u -- ) 0 do c@+ charbuf2 1 swap c! loop drop ; + +: intersect ( -- ) \ mutates charbuf +128 0 do r@ charbuf c@ r@ charbuf2 c@ and r@ charbuf c! loop +; : priority ( c -- u ) 96 mod dup 65 >= if 38 - then ; -: read-rucksack ( -- a n ) pad pad 50 accept ; -: set-charbuf ( a u -- ) 0 do c@+ charbuf 1 swap c! loop drop ; +: read-rucksack ( n -- a n ) 50 * pad + dup 50 accept ; : compartments ( a u -- a1 u1 a2 n2 ) 2 / 2dup swap over + swap ; -: find-duplicate ( a n -- c ) - 0 do c@+ dup charbuf c@ - if nip unloop exit else drop then loop +: get-duplicate ( c ) + 128 0 do r@ charbuf c@ if r@ unloop exit then loop ; + +: set1 compartments +set-charbuf set-charbuf2 intersect +get-duplicate priority total1 +! +clear-buf clear-buf2 ; -: solve 300 0 do - read-rucksack compartments set-charbuf - find-duplicate priority total +! - clear-buf loop - total ? \ part 1 - bye + +: set3 +set-charbuf +set-charbuf2 +intersect clear-buf2 +set-charbuf2 intersect +get-duplicate priority total2 +! +clear-buf clear-buf2 ; -solve bye + +: solve 100 0 do + 0 read-rucksack 2dup set1 + 1 read-rucksack 2dup set1 + 2 read-rucksack 2dup set1 + set3 + loop + total1 ? cr + total2 ? + bye +; solve bye diff --git a/2022/03b.fs b/2022/03b.fs @@ -1,34 +0,0 @@ -\ hacky - -variable total - -: barray create allot does> + ; -: charray 128 barray ; \ range incl a-z 6 symbols A-Z -: c@+ dup c@ >r 1+ r> ; -charray charbuf -charray charbuf2 - -: intersect ( -- ) \ mutates charbuf -128 0 do r@ charbuf c@ r@ charbuf2 c@ and r@ charbuf c! loop -; - -: clear-buf 128 0 do r@ 0 swap charbuf c! loop ; -: clear-buf2 128 0 do r@ 0 swap charbuf2 c! loop ; -: priority ( c -- u ) 96 mod dup 65 >= if 38 - then ; -: read-rucksack ( -- a n ) pad pad 50 accept ; -: set-charbuf ( a u -- ) 0 do c@+ charbuf 1 swap c! loop drop ; -: set-charbuf2 ( a u -- ) 0 do c@+ charbuf2 1 swap c! loop drop ; -: get-duplicate ( c ) - 128 0 do r@ charbuf c@ if r@ unloop exit then loop -; - -: solve 100 0 do - read-rucksack set-charbuf - read-rucksack set-charbuf2 intersect clear-buf2 - read-rucksack set-charbuf2 intersect - get-duplicate priority total +! - clear-buf clear-buf2 loop - total ? - bye -; -solve bye