Paste: AoC day 9
Author: | Garklein |
Mode: | factor |
Date: | Sat, 10 Dec 2022 01:43:49 |
Plain Text |
USING: assocs io.encodings.utf8 io.files kernel math
math.functions math.parser math.vectors prettyprint sequences
sets splitting ;
IN: aoc.9
: input ( -- lines ) "~/factor/aoc/9/9.in" utf8 file-lines ;
: dir>vec ( dir -- vector )
{ { "U" { 0 1 } }
{ "R" { 1 0 } }
{ "D" { 0 -1 } }
{ "L" { -1 0 } } } at ;
: I ( -- coords ) input [ split-words first2 [ dir>vec ] [ dec> ] bi* swap <repetition> ] map
concat { 0 0 } [ v+ ] accumulate* { 0 0 } prefix ;
: move-tail ( tail head -- tail' ) 2dup distance 2 >= [ over v- [ signum ] map v+ ] [ drop ] if ;
: (part) ( head rope-length -- pos-num ) 1 - [ dup first [ move-tail ] accumulate* ] times cardinality ;
: part1 ( -- n ) I 2 (part) ;
: part2 ( -- n ) I 10 (part) ;
: solve ( -- ) part1 . part2 . ;
New Annotation