aoc-forth

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

commit b11760e4b7ae42ffa36bc7eda94d5bc1ef0becef
parent 40f45b8bf82091f75de368348e10726219431fab
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri,  2 Dec 2022 23:38:33 -0800

03 part 1

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

diff --git a/2022/03.fs b/2022/03.fs @@ -0,0 +1,32 @@ +\ read 50 chars +\ first half -> first component +\ second -> second component + +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 + +: clear-buf 58 0 do r@ 0 swap 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@+ asciiA - charbuf 1 swap c! loop drop ; +: compartments ( a u -- a1 u1 a2 n2 ) + 2 / 2dup swap over + swap ; +: find-duplicate ( a n -- c ) + 0 do c@+ dup asciiA - charbuf c@ + if nip unloop exit else drop then loop +; +: solve 300 0 do + read-rucksack compartments set-charbuf + find-duplicate priority total +! + clear-buf loop + total ? \ part 1 + bye +; +solve bye