1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day14.input] day14.m4
4 include(`common.m4')ifelse(common(14), `ok', `',
5 `errprint(`Missing common initialization
8 define(`min', `ifelse(eval($1 < $2), 1, $1, $2)')
9 define(`compute', `$6(`$1', eval($2 * ($3 * ($5 / ($3 + $4)) +
10 min($5 % ($3 + $4), $3))))')
11 define(`phrase1', `define(`$1', 0)`,' dquote(dquote(`$1', $4, $7,')
12 define(`phrase2', `$6))')
13 define(`_slurp', `ifelse(`$#', 1, `', `phrase1(translit(`$1', ` ',
14 `,'))phrase2(translit(`$2', ` ', `,'))$0(shift(shift($@)))')')
15 define(`slurp', `define(`list', _$0$1)')
16 slurp(translit((include(defn(`file'))), `.'nl, `,'))
17 define(`find', `ifelse(eval($2 > max), 1, `define(`max', $2)')')
18 define(`score', `ifelse($2, max, `define(`$1', incr($1))')')
19 define(`do', `compute($1, $2, `$3')')
20 define(`round', `define(`max', 0)_foreach(`do(', `, $1, `find')',
21 list)_foreach(`do(', `, $1, `score')', list)')
22 forloop_arg(1, 2503, `round')
23 define(`part1', max)define(`part2', 0)
24 define(`best', `_$0($1)')
25 define(`_best', `ifelse(eval($1 > part2), 1, `define(`part2', $1)')')