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(`line', `_$0(translit(`$1', ` ', `,'))')
10 define(`_line', `define(`n', $1)ifdef(`G$1', `', `pushdef(`g$1',
11 $1)define(`G$1', $1)')pipe(G$1, shift(shift($@)))')
12 define(`pipe', `ifelse(`$2', `', `', `ifdef(`G$2', `ifelse($1, G$2, `',
13 `merge(ifelse(eval($1 < G$2), 1, `$1, G$2', `G$2, $1'))')', `pushdef(`g$1',
14 $2)define(`G$2', $1)')$0(G$1, shift(shift($@)))')')
15 define(`merge', `ifdef(`g$2', `pushdef(`g$1', g$2)define(`G'g$2,
16 $1)popdef(`g$2')$0($1, $2)')')
18 patsubst(defn(`input'), `\([^;]*\);', `line(`\1')')
20 define(`chew', `line(substr(`$1', 0, index(`$1', `;')))define(`tail',
21 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'), `;'),
22 -1, `', `$0(defn(`tail'))')')
23 define(`split', `ifelse(eval($1 < 90), 1, `chew(`$2')', `$0(eval($1/2),
24 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
25 defn(`tail')substr(`$2', eval($1/2)))')')
26 split(len(defn(`input')), defn(`input'))
29 define(`part1', len(stack_foreach(`g0', `cnt')))
30 define(`part2', len(forloop(0, n, `ifdef(`g'', `, `-')')))