1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dinit=BBB] [-Dfile=day16.input] day16.m4
4 include(`common.m4')ifelse(common(16), `ok', `',
5 `errprint(`Missing common initialization
8 # See https://www.reddit.com/r/adventofcode/comments/5ititq/2016_day_16_c_how_to_tame_your_dragon_in_under_a/
9 ifdef(`init', `', `define(`init', translit(include(defn(`file')), nl))')
10 define(`rev', `ifelse(`$1', `', `', `$0(substr(`$1', 1))ifelse(substr(`$1', 0,
12 define(`p00', 0)define(`p01', 1)define(`p10', 1)define(`p11', 0)
13 define(`n0', 1)define(`n1', 0)
15 define(`prep', `ifelse(`$2', `', `', `define(`i'incr($1),
16 defn(`p'i$1`'substr(`$2', 0, 1)))$0(incr($1), substr(`$2', 1))')')
17 prep(0, init`'rev(init))
18 define(`solve', `_$0(0, eval(`$1 & -$1'), eval(`$1 & -$1'), $1, len(`$2'))')
19 define(`_solve', `define(`p', chunk(eval(`$3 / ($5 + 1)'), $3, $5))defn(
20 `n'defn(`p$1'p))ifelse($3, $4, `', `$0(p, $2, eval($2 + $3), $4, $5)')')
21 define(`chunk', `_$0($1, eval(`($2 - $1) / ($3 * 2)'),
22 eval(`($2 - $1) % ($3 * 2)'), eval(`$1 ^ ($1 >> 1)'), $3)')
23 define(`_chunk', `eval(($4 ^ len(translit(eval($1 & $4, 2), 0)) ^ ($2 & $5)
26 define(`part1', solve(272, init))
27 define(`part2', solve(35651584, init))