1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day2.input] day2.m4
4 include(`common.m4')ifelse(common(2), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(file), nl, `;'))
9 define(`part1', 0)define(`part2', 0)
10 define(`sort', `ifelse(eval($1 < $2), 1, `ifelse(eval($2 < $3), 1, `$1, $2, $3',
11 `$1, $3, $2')', `ifelse(eval($1 < $3), 1, `$2, $1, $3', `$2, $3, $1')')')
12 define(`line', `_$0(sort($@))')
13 define(`_line', `define(`part1', eval(part1 + 3 * $1 * $2 + 2 * $1 * $3 +
14 2 * $2 * $3))define(`part2', eval(part2 + 2 * ($1 + $2) + $1 * $2 * $3))')
16 patsubst(defn(`list'), `\([0-9]*\)x\([0-9]*\)x\([0-9]*\);',
19 define(`chew', `line(translit(substr(`$1', 0, index(`$1', `;')), `x',
20 `,'))define(`tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
21 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
22 define(`split', `ifelse(eval($1 < 20), 1, `chew(`$2')', `$0(eval($1/2),
23 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
24 defn(`tail')substr(`$2', eval($1/2)))')')
25 split(len(defn(`list')), defn(`list'))