1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day16.input] day16.m4
4 include(`common.m4')ifelse(common(16), `ok', `',
5 `errprint(`Missing common initialization
8 define(`r', `r$1')define(`i', `$1_')define(`z', `0_')define(`_')
9 define(`r0', `$1')define(`r1', `$2')define(`r2', `$3')define(`r3', `$4')
10 define(`o0', ``r',+,`r'')define(`o1', ``r',+,`i'')
11 define(`o2', ``r',*,`r'')define(`o3', ``r',*,`i'')
12 define(`o4', ``r',&,`r'')define(`o5', ``r',&,`i'')
13 define(`o6', ``r',|,`r'')define(`o7', ``r',|,`i'')
14 define(`o8', ``r',-,`z'')define(`o9', ``i',-,`z'')
15 define(`o10', ``i',>,`r'')define(`o11', ``r',>,`i'')
16 define(`o12', ``r',>,`r'')define(`o13', ``i',==,`r'')
17 define(`o14', ``r',==,`i'')define(`o15', ``r',==,`r'')
18 define(`o', `eval($1($4)$6$2$3($5)$6)')
19 define(`test', `ifelse(eval(len('forloop(0, 15, ``_$0('', ``$'@`)'')`) >= 3),
20 1, `define(`part1', incr(part1))')')
21 define(`_test', `ifelse(o(o$1(), $4, $5, $2), r$6$9, -, `define(`p$3',
22 eval(p$3 & ~(1 << $1)))')')
23 define(`s0', `define(`R', `$5,$2,$3,$4')')
24 define(`s1', `define(`R', `$1,$5,$3,$4')')
25 define(`s2', `define(`R', `$1,$2,$5,$4')')
26 define(`s3', `define(`R', `$1,$2,$3,$5')')
27 define(`R', `0,0,0,0')
28 forloop(0, 16, `define(`p'', `, `65535')')
29 define(`settle', `ifelse(p16, 0, `', `forloop_arg(0, 15, `_$0')$0()')')
30 define(`_settle', `$0_((p$1&p16), $1)')
31 define(`_settle_', `ifelse(eval(`$1&&($1&-$1)==$1'), 1, `define(`p16',
32 eval(p16&~$1))define(`m$2', `o'decr(len(eval(`$1', 2))))')')
34 define(`Before')define(`After')
35 define(`input', translit((include(defn(`file'))), nl`,:()', `;'))
36 define(`init', substr(defn(`input'), 0, incr(incr(index(defn(`input'),
38 define(`list', substr(defn(`input'), eval(4+index(defn(`input'), `;;;;'))))
40 define(`visit', `test(translit(`$1', ` ;[]', `,,()'))')
41 define(`op', `s$4(R, o(m$1, $2, $3, (R)))')
43 patsubst(defn(`init'), `\([^;]*;[^;]*;[^;]*\);;', `visit(`\1')')
45 define(`d', `\([0-9][0-9]*\)')
46 patsubst(defn(`list'), d` 'd` 'd` 'd`;', `op(`\1', `\2', `\3', `\4')')
48 define(`_chew', `visit(substr(`$1', 0, index(`$1', `;;')))define(`tail',
49 substr(`$1', incr(incr(index(`$1', `;;')))))ifelse(index(defn(`tail'),
50 `;;'), -1, `', `$0(defn(`tail'))')')
51 define(`chew', `ifelse(eval(`$1 < 85'), 1, `_$0(`$2')', `$0(eval(`$1/2'),
52 substr(`$2', 0, eval(`$1/2')))$0(eval(len(defn(`tail'))` + $1 - $1/2'),
53 defn(`tail')substr(`$2', eval(`$1/2')))')')
54 chew(len(defn(`init')), defn(`init'))
56 define(`_chew', `op(translit(substr(`$1', 0, index(`$1', `;')), ` ',
57 `,'))define(`tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(
58 defn(`tail'), `;'), -1, `', `$0(defn(`tail'))')')
59 define(`chew', `ifelse(eval(`$1 < 20'), 1, `_$0(`$2')', `$0(eval(`$1/2'),
60 substr(`$2', 0, eval(`$1/2')))$0(eval(len(defn(`tail'))` + $1 - $1/2'),
61 defn(`tail')substr(`$2', eval(`$1/2')))')')
62 chew(len(defn(`list')), defn(`list'))
64 define(`part2', r0(R))