Paste: aoc 9
Author: | Kren/chunes |
Mode: | factor |
Date: | Fri, 9 Dec 2022 15:48:32 |
Plain Text |
USING: arrays assocs io.encodings.ascii io.files kernel literals
math math.extras math.functions math.parser math.vectors
pair-rocket prettyprint sequences sets splitting ;
IN: aoc2022.day09
CONSTANT: input
$[ "vocab:aoc2022/day09/input.txt" ascii file-lines ]
CONSTANT: dirs
H{
"U" => { -1 0 } "D" => { 1 0 }
"L" => { 0 -1 } "R" => { 0 1 }
}
: seen ( -- obj ) HS{ } ;
: visit ( obj -- ) seen adjoin ;
: tally ( -- n ) seen cardinality ;
: near? ( u v -- ? ) v- [ [-1,1]? ] all? ;
: yank ( u v -- w ) tuck [ - sgn ] 2map v+ ;
: tug ( u v -- w ) 2dup near? [ nip ] [ yank ] if ;
: parse ( str -- n v ) " " split1 dec> swap dirs at ;
: pluck ( seq u -- seq v ) swap unclip rot v+ ;
: trail ( seq u -- v seq ) pluck tuck [ tug ] accumulate* ;
: drag ( seq v -- seq ) trail swap prefix dup last visit ;
: reset ( -- ) seen clear-set ;
: line ( seq str -- seq ) parse '[ _ drag ] times ;
: pull ( seq seq -- ) [ line ] each drop ;
: knots ( n -- seq ) { 0 0 } <array> ;
: sim ( n -- ) knots input pull tally . ;
: part1 ( -- ) 2 sim ;
: part2 ( -- ) 10 sim ;
: day09 ( -- ) part1 reset part2 ;
MAIN: day09
New Annotation