1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day17.input] day17.m4
3 # Optionally use -Dverbose=1 to see some progress
5 include(`common.m4')ifelse(common(17), `ok', `',
6 `errprint(`Missing common initialization
9 define(`parse', `define(`minx', `$2')define(`maxx', $4)define(`miny',
10 $6)define(`maxy', $8)')
11 parse(translit((include(defn(`file'))), `=.()'nl, `,,'))
12 define(`triangle', `eval(($1)*(($1)+1)/2)')
13 define(`lowx', `ifelse(eval(triangle($1) >= minx), 1, $1, `$0(incr($1))')')
15 ifelse(eval(triangle(x) <= maxx), 1, `define(`part1', triangle(-1 - miny))')
17 define(`_good', `ifelse(eval($1 > maxx || $2 < miny), 1, `0',
18 eval($1 >= minx && $1 <= maxx && $2 <= maxy && $2 >= miny), 1, `$5',
19 eval($3 == 0 && $1 < minx), 1, `0', `$0(eval($1 + $3), eval($2 + $4),
20 ifelse($3, 0, 0, decr($3)), decr($4), incr($5))')')
21 define(`good', `define(`t', _$0(0, 0, $@, 0))ifelse(t, 0, `', `define(`part2',
22 incr(part2))ifdef(`part1', `', `define(`best', $2)')')output(1, `$1, $2: 't)')
23 define(`try', `forloop(x, maxx, `good(', `, $1)')')
24 forloop_arg(miny, eval(-1 - miny), `try')
25 ifdef(`best', `define(`part1', triangle(best))')