Paste: aoc2
Author: | Krenium |
Mode: | factor |
Date: | Wed, 4 Dec 2019 18:08:15 |
Plain Text |
USING: combinators continuations fry io.encodings.ascii io.files
kernel math math.parser prettyprint sequences splitting ;
IN: aoc.2019.02
CONSTANT: instruction-offset 4
CONSTANT: halt-opcode 99
ERROR: unknown-opcode n ;
: opcode>quot
{
{ 1 [ [ + ] ] }
{ 2 [ [ * ] ] }
[ unknown-opcode ]
} case ;
: intcode>seq "," split [ string>number ] map ;
: meta-nth [ nth ] keep nth ;
: under+ [ + ] curry dip ;
: at-address under+ meta-nth ;
: execute-instruction
[ instruction-offset * ] dip tuck {
[ 1 at-address ]
[ 2 at-address ]
[ nth opcode>quot call ]
[ 3 under+ nth ]
[ nip set-nth ]
} 2cleave ;
: get-opcode [ instruction-offset * ] dip nth ;
: (run-intcode)
0 swap [ 2dup get-opcode halt-opcode = ]
[ dupd execute-instruction 1 under+ ] until nip ;
: set-nth* rot [ set-nth ] keep ;
: set-noun 1 set-nth* ;
: set-verb 2 set-nth* ;
: run-intcode
[ intcode>seq ] [ set-noun ] [ set-verb ] tri*
(run-intcode) ;
: parse-input
"resource:work/aoc/2019/02/input.txt" ascii file-contents ;
: part1 parse-input 12 2 run-intcode first . ;
: part2
[
100 <iota> dup parse-input
[ -rot [ run-intcode ] 2keep pick first 19690720 =
[ [ 100 * ] dip + . drop return ] [ 3drop ] if ]
curry cartesian-each
] with-return ;
New Annotation