1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day8.input] day8.m4
4 include(`common.m4')ifelse(common(8), `ok', `',
5 `errprint(`Missing common initialization
8 # Gag: some input files happen to contain macro names
9 pushdef(`nl', `ifelse(`$#', 0, ``$0'', `''nl`)')
10 define(`list', translit(include(defn(`file')), nl(), `;'))
11 define(`part1', len(defn(`list')))
12 define(`part2', eval(2 * part1 - len(translit(defn(`list'), `;')) -
13 len(translit(defn(`list'), `\";'))))
14 define(`_visit', `popdef(`$0')ifelse(`$1', `x', `pushdef(`$0',
15 `popdef(`$0')')pushdef(`$0', `popdef(`$0')')')define(`part1', decr(part1))')
16 define(`visit', `ifelse(`$1', `\', `pushdef(`$0', defn(`_$0'))', `$1', `"',
17 `', `define(`part1', decr(part1))')')
19 patsubst(defn(`list'), `\(.\)', `visit(`\1')')
21 define(`split', `ifelse($1, 1, `visit(`$2')', `$0(eval($1/2),
22 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
23 defn(`tail')substr(`$2', eval($1/2)))')')
24 split(len(defn(`list')), defn(`list'))