1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day23.input] day23.m4
4 include(`common.m4')ifelse(common(23), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit((include(defn(`file'))), nl`,()', `;'))
10 define(`line', `_$0(translit(`$1', ` ', `,'))')
11 define(`_line', `define(`i'cnt, `$1')define(`a'cnt, quote(shift($@),
12 cnt))define(`cnt', incr(cnt))')
14 patsubst(defn(`input'), `\([^;]*\);', `line(`\1')')
16 define(`_chew', `line(substr(`$1', 0, index(`$1', `;')))define(`tail',
17 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'), `;'), -1,
18 `', `$0(defn(`tail'))')')
19 define(`chew', `ifelse(eval($1 < 20), 1, `_$0(`$2')', `$0(eval($1/2),
20 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
21 defn(`tail')substr(`$2', eval($1/2)))')')
22 chew(len(defn(`input')), defn(`input'))
25 define(`r_', `ifelse(len(`$1'), 1, `ifdef(`r$1', `r')')$1')
26 define(`cpy_', `define(`r$2', r_(`$1'))1')
27 define(`inc_', `define(`r$1', incr(r$1))1')
28 define(`dec_', `define(`r$1', decr(r$1))1')
29 define(`jnz_', `ifelse(r_(`$1'), 0, 1, $2, -2, `hackadd(`$1', `$2',
30 decr(decr($3)), decr($3))', $2, -5, `hackmul(`$1', `$2'forloop(eval($3 - 5),
31 decr($3), `,'))', `r_(`$2')')')
32 define(`hackadd', `ifelse(i$3.i$4, `dec.inc', `$0(`$1', `$2', $4, $3)',
33 i$3.i$4.first(a$4), `inc.dec.$1', `define(`r'first(a$3),
34 eval(defn(`r'first(a$3)) + r$1))define(`r$1', 0)1', `r_(`$2')')')
35 define(`hackmul', `ifelse(i$3.i$4.i$5.i$6.i$7, `cpy.inc.dec.jnz.dec',
36 `define(`r'first(a$4), eval(defn(`r'first(a$4)) + r_(first(a$3)) *
37 r$1))define(`r'first(a$5), 0)define(`r$1', 0)1', `r_(`$2')')')
38 define(`tgl_', `toggle(eval($2 + r_(`$1')))1')
39 define(`toggle', `ifdef(`i$1', `pushdef(`mod', `i$1')pushdef(`i$1',
46 define(`run', `ifdef(`i$1', `$0(_$0(i$1, $1))')')
47 define(`_run', `eval($2 + $1_(a$2))')
48 define(`reset', `ifdef(`mod', `popdef(defn(`mod'))popdef(`mod')$0()',
49 `define(`ra', 0)define(`rb', 0)define(`rc', 0)define(`rd', 0)')')
51 reset()define(`ra', 7)
54 reset()define(`ra', 12)