Paste: AOC day 2
Author: | chunes |
Mode: | factor |
Date: | Thu, 2 Dec 2021 06:11:55 |
Plain Text |
USING: accessors arrays assocs assocs.extras io.encodings.ascii
io.files kernel literals math math.parser prettyprint sequences
splitting words ;
IN: aoc.2021.02
CONSTANT: instructions $[
"input.txt" ascii file-lines [ " " split1 dec> 2array ] map
]
: part1
instructions expand-values-push-at [ sum ] map-values
[ "down" of ] [ "up" of - ] [ "forward" of * . ] tri ;
TUPLE: position aim horizontal depth ;
: <position> 0 0 0 position boa ;
: down '[ _ + ] change-aim drop ;
: up '[ _ - ] change-aim drop ;
: forward-h '[ _ + ] change-horizontal drop ;
: forward-a over aim>> * '[ _ + ] change-depth drop ;
: forward [ forward-h ] [ forward-a ] 2bi ;
: do-command
first2 swap "aoc.2021.02" lookup-word execute ;
: do-commands
<position> instructions [ dupd do-command ] each ;
: part2
do-commands [ depth>> ] [ horizontal>> ] bi * . ;
part1 part2
New Annotation