1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day12.input] day12.m4
4 include(`common.m4')ifelse(common(12), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit((include(defn(`file'))), `,()'nl, `;'))
9 define(`scrubbed', translit(defn(`input'), `:"abcdefghijklmnopqrstuvwxyz[]{}',
11 define(`part1', 0)define(`part2', 0)define(`Red')define(`Num')
12 define(`str', `ifelse(`$1', `"', `popdef(`$0')ifelse(defn(`Str')Value, `red-',
13 `define(`Red', -)')define(`Value')', `define(`Str', defn(`Str')`$1')')')
14 define(`lex', `ifelse(translit(`$1', `-0123456789'), `', `define(`Num',
15 defn(`Num')`$1')', defn(`Num'), `', `', `define(`part2', eval(Num +
16 part2))define(`Num')')ifelse(`$1', `:', `define(`Value', -)', `$1', `"',
17 `define(`Str')pushdef(`$0', defn(`str'))', `$1', `{', `pushdef(`part2',
18 0)pushdef(`Red')define(`Value')', `$1', `}', `ifelse(Red, `',
19 `define(`part2', eval(part2 + popdef(`part2')part2))', `popdef(
20 `part2')')popdef(`Red')define(`Value')', `define(`Value')')')
22 define(`part1', eval(patsubst(defn(`scrubbed'), `;', `+ ')))
23 patsubst(defn(`input'), `.', `lex(`\&')')
25 define(`add', `ifelse(`$1', `', `', `define(`part1', eval(part1 + $1))')')
26 define(`chew', `add(substr(`$1', 0, index(`$1', `;')))define(`tail',
27 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
28 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
29 define(`split', `ifelse(eval($1 < $3), 1, `$4(`$2')', `$0(eval($1/2),
30 substr(`$2', 0, eval($1/2)), `$3', `$4')$0(eval(len(defn(`tail'))
31 + $1 - $1/2), defn(`tail')substr(`$2', eval($1/2)), `$3', `$4')')')
32 split(len(defn(`scrubbed')`;'), defn(`scrubbed')`;', 10, `chew')
33 split(len(defn(`input')), defn(`input'), 2, `lex')