1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day9.input] day9.m4
4 include(`common.m4')ifelse(common(9), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit((include(defn(`file'))), `,`'()'nl, `;[]'))
9 define(`depth', 0)define(`part1', 0)define(`part2', 0)
10 define(`visitc', `popdef(`$0')')
11 define(`visitg', `ifelse(`$1', `>', `popdef(`$0')', `$1', `!', `pushdef(`$0',
12 defn(`visitc'))', `define(`part2', incr(part2))')')
13 define(`visit', `ifelse(`$1', `{', `define(`depth', incr(depth))define(`part1',
14 eval(part1 + depth))', `$1', `}', `define(`depth', decr(depth))', `$1', `<',
15 `pushdef(`$0', defn(`visitg'))')')
17 patsubst(defn(`input'), `.', `visit(`\&')')
19 define(`split', `ifelse($1, 1, `visit(`$2')', `$0(eval($1/2),
20 substr(`$2', 0, eval($1/2)), `$3')$0(eval($1 - $1/2), substr(`$2',
21 eval($1/2)), `$3')')')
22 split(len(defn(`input')), defn(`input'))