Paste: AOC 2026, day 05

Author: gifti
Mode: factor
Date: Sat, 5 Dec 2026 16:19:11
Plain Text |
USING: assocs deques dlists kernel math math.combinatorics
math.parser multiline peg.ebnf ranges sequences sets splitting
strings ;
IN: 2026.05

: op ( str ch quot -- str' )
    [ 1string [ dec> ] bi@ ] dip call >dec ; inline

EBNF: parse-button [=[
    d = [0-9]
    replace = d "->"~ d => [[ '[ { _ } substitute ] ]]
    append = d => [[ '[ _ over "0" = [ nip 1string ] [ suffix ] if ] ]]
    multiply = "*"~ d => [[ '[ _ [ * ] op ] ]]
    divide = "/"~ d => [[ '[ _ [ /i ] op ] ]]
    octal = "oct" => [[ [ dec> >oct ] ]]
    bksp = "bksp" => [[ [ but-last [ "0" ] when-empty ] ]]
    button = "["~ (replace|append|multiply|divide|octal|bksp) "]"~
]=]

: parse ( seq -- buttons )
    split-words [ parse-button ] map ;

: part-1 ( buttons -- n )
    [ HS{ } clone ] dip 3 [0..b] [
        [
            "0" [ call( str -- str' ) ] reduce over adjoin
        ] each-selection
    ] with each members [ dec> ] map-sum ;

: part-2 ( buttons -- n )
    [ HS{ } clone "0" 1dlist 2dup ] dip swap '[
        dup _ ?adjoin [
            _ [
                call( str -- str' )
                dup length 4 > [ drop ] [ _ push-front ] if
            ] with each
        ] [ drop ] if
    ] slurp-deque members [ dec> ] map-sum ;

New Annotation

Summary:
Author:
Mode:
Body: