Paste: AoC 2015 Day 6 Part 2

Author: greenfork
Mode: factor
Date: Sat, 2 Sep 2023 06:55:15
Plain Text |
USING: kernel sequences ranges math peg.ebnf multiline assocs math.parser
io io.files io.encodings.ascii generalizations arrays pair-rocket math.order
combinators prettyprint ;
IN: aoc.2015.06-script

EBNF: grammar [=[
verb = "turn on" | "turn off" | "toggle"
digit = [0-9]+ => [[ string>number ]]
coord = digit:a "," digit:b => [[ a b 2array ]]
main = verb " "~ coord " through "~ coord
]=]

! Given a coordinate, return a position in the grid corresponding to this cell.
: pos ( coord -- n ) first2 1000 * + ;

: (apply) ( coord grid str -- )
    [ pos ] 2dip {
        "turn on" => [ [ 1 + ] change-nth ]
        "turn off" => [ [ 1 - 0 max ] change-nth ]
        "toggle" => [ [ 2 + ] change-nth ]
    } case ;

: apply ( grid str -- 'grid )
    grammar first3 [ first2 ] bi@ swapd [ [a..b] ] 2bi@
    rot 4 npick swap '[ 2array _ _ (apply) ] cartesian-each ;

"resource:work/aoc/2015/06/input" ascii [
    1,000,000 0 <array>
    [ readln ] [ apply ] while* sum
] with-file-reader .

New Annotation

Summary:
Author:
Mode:
Body: