Paste: AoC 2022 Day 9

Author: Kacarott
Mode: factor
Date: Fri, 9 Dec 2022 08:47:51
Plain Text |
! Copyright (C) 2022 Keldan Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel AOC prettyprint sequences splitting math
       math.vectors sets math.parser combinators ;
IN: AOC.2022.9


: chase ( u v -- u' ) swap 2dup distance 1.5 >
    [ over v- [ sgn ] map v+ ] [ drop ] if ;

: record ( r u -- r u ) [ last over adjoin ] keep ;

: step ( r u c -- r u ) unclip [ rest dec> ] dip {
        { CHAR: L [ { 0 -1 } ] } { CHAR: R [ { 0 1 } ] }
        { CHAR: U [ { -1 0 } ] } { CHAR: D [ { 1 0 } ] }
    } case '[ unclip _ v+ [ chase ] accumulate swap suffix record ] times ;

: solution ( input n -- result )
    '[ HS{ } clone _ [ { 0 0 } clone ] replicate ] dip
    split-lines [ step ] each drop cardinality ;

: part-1 ( input -- result ) 2 solution ;
: part-2 ( input -- result ) 10 solution ;

MAIN: [ 9 read-day-input [ part-1 . ] [ part-2 . ] bi ]

New Annotation

Summary:
Author:
Mode:
Body: