1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day08.input] day08.m4
4 include(`common.m4')ifelse(common(08), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit(include(defn(`file')), nl, `;'))
9 define(`x', 0)define(`y', 0)define(`part1', 0)define(`part2', 0)
10 define(`do', `ifelse($3, `;', `define(`maxx', decr($1))define(`x',
11 0)define(`y', incr($2))', `define(`g$1_$2', $3)define(`s$1_$2', 1)define(`x',
15 patsubst(defn(`input'), `.', `do(x, y, `\&')')
17 define(`chew', `ifelse($1, 1, `do(x, y, `$2')', `$0(eval($1/2), substr(
18 `$2', 0, eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
19 chew(len(defn(`input')), defn(`input'))
22 define(`maxy', decr(y))
23 ifelse(maxx, maxy, `', `fatal(`input is not square')')
25 define(`bump1', `ifdef(`v$1', `', `define(`v$1')define(`part1', incr(part1))')')
26 define(`_bump2', `define(`s$1', `$2')ifelse(eval(`$2>'part2), 1,
27 `define(`part2', `$2')')')
28 define(`bump2', `_$0(`$1', eval(s$1`*$2'))')
29 define(`view', `ifelse(eval(s1$2`<$1'), 1, `popdef(`s1$2')popdef(`s2$2')$0($@)')')
30 define(`_probe', `ifelse(eval(`$2>'v$4), 1, `bump1(`$1')define(`v$4', $2)')view(
31 $2, `$4')bump2(`$1', ($3-s2$4))pushdef(`s1$4', `$2')pushdef(`s2$4', `$3')')
32 define(`probe', `ifelse($2, $3, `', `_$0(`$1_$2', g$1_$2, $5, `x')_$0(`$2_$1',
33 g$2_$1, $5, `y')$0($1, $4($2), $3, `$4', incr($5))')')
34 define(`prep', `define(`v$1', -1)define(`s1$1', 10)define(`s2$1', 0)')
35 forloop(0, maxx, `prep(`x')prep(`y')probe(', `, 0, 'maxy`, `incr', 0)')
36 forloop(0, maxx, `prep(`x')prep(`y')probe(', `, 'maxy`, 0, `decr', 0)')