aoc-forth

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

commit dd415be90cd8017e49f137e052d531638db91f9a
parent b11760e4b7ae42ffa36bc7eda94d5bc1ef0becef
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  3 Dec 2022 10:49:45 -0800

part b of day 3

Diffstat:
M2022/03.fs | 4----
A2022/03b.fs | 36++++++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/2022/03.fs b/2022/03.fs @@ -1,7 +1,3 @@ -\ read 50 chars -\ first half -> first component -\ second -> second component - 65 constant asciiA variable total diff --git a/2022/03b.fs b/2022/03b.fs @@ -0,0 +1,36 @@ +\ hacky + +65 constant asciiA +variable total + +\ byte array +: barray create allot does> + ; +: charray 58 barray ; \ range incl a-z 6 symbols A-Z +: c@+ dup c@ >r 1+ r> ; +charray charbuf +charray charbuf2 + +: intersect ( -- ) \ mutates charbuf +58 0 do r@ charbuf c@ r@ charbuf2 c@ and r@ charbuf c! loop +; + +: clear-buf 58 0 do r@ 0 swap charbuf c! loop ; +: clear-buf2 58 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@+ asciiA - charbuf 1 swap c! loop drop ; +: set-charbuf2 ( a u -- ) 0 do c@+ asciiA - charbuf2 1 swap c! loop drop ; +: get-duplicate ( c ) + 58 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 asciiA + priority total +! + total ? + clear-buf clear-buf2 loop + bye +; +solve bye