aoc-forth

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

commit b43fe55bc8eba3007954418e6a217e029ddedef4
parent 6f0eba0d9e2c1a668eaca6327a29963ce72e04e3
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  3 Dec 2022 23:23:39 -0800

day 4 rough

Diffstat:
M2022/03.fs | 1+
A2022/04.fs | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/2022/03.fs b/2022/03.fs @@ -7,6 +7,7 @@ variable total2 128 barray charbuf 128 barray charbuf2 +\ TODO use erase : 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 ; diff --git a/2022/04.fs b/2022/04.fs @@ -0,0 +1,45 @@ +\ a b -- a includes b +\ end is greater than end and start is less than start +\ end is less than start OR start is less than end +45 constant dash +44 constant comma + +: 4dup 3 pick 3 pick 3 pick 3 pick ; +: not-num ( u -- f ) dup 0 < swap 9 > or ; + +\ reads until nonnumeric char +: to-num ( a u -- n ) + 0 -rot 0 do dup r@ + c@ h# 30 - + dup not-num if unloop drop drop exit then + rot 10 * + swap loop drop ; + +: includes ( u1 u2 u3 u4 -- f ) rot <= -rot <= and ; +: overlaps ( u1 u2 u3 u4 -- f ) -rot >= -rot <= and ; +: either-overlaps ( u1 u2 u3 u4 -- f ) + 4dup overlaps >r 2swap overlaps r> and ; +: either-includes ( u1 u2 u3 u4 -- f ) + 4dup includes >r 2swap includes r> or ; +: read-str pad 12 erase pad pad 12 accept ; +: read-pairs ( -- u1 u2 u3 u4 ) +read-str +\ uggo +2dup to-num -rot dash scan swap 1+ swap +2dup to-num -rot comma scan swap 1+ swap +2dup to-num -rot dash scan swap 1+ swap +to-num +; + +variable total1 +variable total2 +: solve + 1000 0 do + read-pairs + 4dup + either-includes if 1 total1 +! then + .s overlaps .s cr if 1 total2 +! then + loop + total1 ? cr \ part 1 + total2 ? \ part 1 + bye +; +solve