1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day23.input] day23.m4
3 # Optionally use -Dverbose=[12] to see some progress
5 include(`intcode.m4')ifelse(intcode(23), `ok', `',
6 `errprint(`Missing IntCode initialization
10 define(`prep', `oneshot(`io', defn(`pause_on_read'))
11 oneshot(`io', defn(`run_after_read'))
12 oneshot(`read', $1)copy($1)runs($1)')
13 forloop_arg(0, 49, `prep')
16 define(`write', `define(`data', $1)')
17 define(`swap', `ifdef(`$1', `pushdef(`$2', defn(`$1'))popdef(`$1')$0($@)')')
19 swap(`q$1y', `t')oneshot(`read', t)popdef(`t')swap(`t', `q$1y')
20 swap(`q$1x', `t')oneshot(`read', t)popdef(`t')swap(`t', `q$1x')')
21 define(`io', `ifelse($1, 0,
22 `ifdef(`q's`x', `consume(s)oneshot(`io', defn(`run_after_read'))
23 oneshot(`io', defn(`run_after_read'))run(pc)', `oneshot(`io')run($2)')',
24 `define(`a', data)ifelse(a, 255, `',
25 `oneshot(`check', a)')oneshot(`io',
26 defn(`pause_after_write'))run($2)pushdef(`q'a`x', data)oneshot(`io',
27 defn(`pause_after_write'))run(pc)pushdef(`q'a`y', data)run(pc)')')
28 define(`i', 0)define(`j', 0)
29 define(`iter', `output(1, `iteration '$1)define(`$1', incr($1))')
30 define(`_loop', `ifdef(`check', `runs(check)_loop()')')
31 define(`loop', `iter(`i')_$0()ifdef(`part1', `', `define(`part1',
32 q255y)')ifelse(part2, q255y, `oneshot(`loop')', `define(`part2',
33 q255y)')pushdef(`q0x', q255x)pushdef(`q0y', q255y)oneshot(`check', 0)loop()')
34 forloop_arg(0, 49, `runs')
35 forloop_arg(0, 49, `runs')