Paste: AoC 2022 day 10
Author: | xr |
Mode: | factor |
Date: | Tue, 13 Dec 2022 18:58:00 |
Plain Text |
USING: strings arrays.shaped prettyprint aoc kernel ranges match peg.ebnf multiline sequences math.vectors math math.parser ;
IN: aoc2022-10
SYMBOLS: 1add addx noop ;
EBNF: parse [=[
n = "-"? [0-9]+ => [[ concat dec> ]]
addx = "addx" " "~ n:v => [[ { 1add { addx v } } ]]
noop = "noop" => [[ { noop } ]]
row = addx | noop
]=]
: small-input ( -- n )
"work/aoc2022-10/small-input.txt" load-file ;
: big-input ( -- n )
"work/aoc2022-10/big-input.txt" load-file ;
: input ( -- n )
"work/aoc2022-10/input.txt" load-file ;
MATCH-VARS: ?value ;
: instr ( state inst -- state )
{
{ noop [ ] }
{ 1add [ ] }
{ { addx ?value } [ ?value + ] }
} match-cond ;
: execution-trace ( seq(instr) -- trace )
1 [ instr ] accumulate swap suffix ;
: cycles ( -- seq )
{ 20 60 100 140 180 220 } ;
: answer ( trace -- n )
cycles 1 v-n swap nths
cycles [ * ] 2map sum
;
: part1 ( -- n )
input [ parse ] map concat
execution-trace
answer ;
: write-pixels ( seq -- seq )
[ ] [ length [0..b) ] bi
[ 40 mod - abs 2 < CHAR: # CHAR: . ? ] 2map ;
: draw-screen ( seq -- )
1 head*
{ 6 40 } reshape
[ >string . ] each-row ;
: part2 ( -- )
input [ parse ] map concat
execution-trace
write-pixels
draw-screen ;
New Annotation