1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day15.input] day15.m4
4 include(`intcode.m4')ifelse(intcode(15), `ok', `',
5 `errprint(`Missing IntCode initialization
9 define(`try', `try_$1()')
14 define(`right', `define(`dir', right_$1())')
15 define(`right_n', `e')
16 define(`right_e', `s')
17 define(`right_s', `w')
18 define(`right_w', `n')
19 define(`left', `define(`dir', left_$1())')
24 define(`move', `_move($1, incr(g($1, x, y))move_$2(), x, y)')
25 define(`_move', `ifdef(`g$1_$3_$4', `', `define(`g$1_$3_$4',
26 $2)ifelse(eval($2 > part$1), 1, `define(`part$1', $2)')')')
27 define(`move_n', `define(`y', decr(y))')
28 define(`move_e', `define(`x', incr(x))')
29 define(`move_s', `define(`y', incr(y))')
30 define(`move_w', `define(`x', decr(x))')
31 define(`g', `ifdef(`g$1_$2_$3', `g$1_$2_$3')')
33 define(`read', `try(dir)')
34 define(`write', `ifelse($1, 2, `oneshot(`loop')')define(`data', $1)')
36 oneshot(`io', defn(`pause_after_write'))
37 oneshot(`io', defn(`run_after_read'))
39 ifelse(data, 0, `right(dir)',
40 `move($1, dir)ifelse(eval(x & y & 1), 1, `left(dir)')')
49 define(`part1', g(1, x, y))