-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.q
36 lines (27 loc) · 755 Bytes
/
2.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/ 2.q
/ Advent of Code 2019
/ Public domain as declared by Sturm Mabie
\l 0.q
init:("J"$) each "," vs (read0 `:2.txt)[0] / instruction data
output:19690720 / magic number to find for part 2
tape:0#0
seq:til count init
comb:seq cross seq
/ run the instruction at address x
step:{if[x=-1; :-1];
if[99=op:tape[x]; :-1];
src1:tape[x+1]; src2:tape[x+2]; dst:tape[x+3];
tape[dst]:$[op=1; +; *][tape[src1]; tape[src2]];
x+4}
/ run the machine until we converge on -1, resetting the tape each run
run:{[xs]
tape::init;
tape[1]:xs[0]; tape[2]:xs[1];
step scan 0;
ret:tape[0];
ret}
/ find the noun and verb that produces output and compute the magic number
find:{sum (100; 1)*comb[(run each comb)?output]}
part1 run (12; 2)
part2 find[]
exit 0