1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day19.input] day19.m4
4 include(`common.m4')ifelse(common(19), `ok', `',
5 `errprint(`Missing common initialization
8 # The input contains the substring nl, but no upper case other than
9 # "A" or "R"; "in" as the only node name with lower case vowels, and
10 # bare "a" for a category. Rename the macros that would interfere.
11 define(`Nl', defn(`nl'))undefine(`nl')
12 define(`Dnl', defn(`dnl'))undefine(`dnl')
13 define(`errprintn', `errprint(`$1'Nl())')
15 define(`x', `$`1'')define(`m', `$`2'')define(`a', `$`3'')define(`s', `$`4'')
16 define(`input', translit((include(defn(`file'))), Nl`,()', `;.'))
17 popdef(`x')popdef(`m')popdef(`a')popdef(`s')
18 define(`part1', 0)define(`part2', 0)
20 define(`A_', `define(`part1', eval(part1+$1+$2+$3+$4))')
21 define(`use', `ifelse(`$3', `', `$2_($1)', `ifelse(eval($2), 1, `$3_($1)',
22 `$0(`$1', shift(shift(shift($@))))')')')
23 define(`run', `in_($2,$4,$6,$8)')
24 define(`do', `run(translit(`$1', `.=${}', `,,'))')
25 define(`build', `define(`$2_', `use'(``$$1','shift(shift($@))))')
26 pushdef(`do', `ifelse(`$1', `', `popdef(`$0')', `build(`@', translit(`$1',
30 patsubst(defn(`input'), `\([^;]*\);', `do(`\1')')
32 define(`_chew', `do(substr(`$1', 0, index(`$1', `;')))define(
33 `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'),
34 `;'), -1, `', `$0(defn(`tail'))')')
35 define(`chew', `ifelse(eval($1 < 145), 1, `_$0(`$2')', `$0(eval($1/2),
36 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
37 defn(`tail')substr(`$2', eval($1/2)))')')
38 chew(len(defn(`input')), defn(`input'))
42 define(`diff', `($3-$2+1)')
43 define(`A_', `define(`part2', add64(part2, mul64(eval(diff$1*diff$2),
44 eval(diff$3*diff$4))))')
45 define(`cmp', `eval($1$4)eval($2$4), $1, $2, $3')
46 define(`_fork', `ifelse($3, 00, `,`$1(,$4,$5,$6,)$2'', $3, 11,
47 ``$1(,$4,$5,$6,)$2',', $3, 01, ``$1(,incr($7),$5,$6,)$2',`$1(,$4,$7,$6,)$2'',
48 ``$1(,$4,decr($7),$6,)$2',`$1(,$7,$5,$6,)$2'')')
49 define(`fork', `_$0(fork$7($1, $2, $3, $4, $8), substr($8, 1))')
50 define(`fork1', ``', `,$2,$3,$4', cmp(shift$1$5)')
51 define(`fork2', ``$1,', `,$3,$4', cmp(shift$2$5)')
52 define(`fork3', ``$1,$2,', `,$4', cmp(shift$3$5)')
53 define(`fork4', ``$1,$2,$3,', `', cmp(shift$4$5)')
54 define(`_use', `ifelse(`$1', `', `', `$3_($1)')use(`$2',
55 shift(shift(shift($@))))')
56 define(`use', `ifelse(`$1', `', `', `$3', `', `$2_($1)', `_$0(fork($1,
57 shift$2), shift(shift($@)))')')
58 in_((,1,4000,1,), (,1,4000,2,), (,1,4000,3,), (,1,4000,4,))