1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day16.input] day16.m4
3 # Optionally use -Dverbose=1 to see some progress
5 include(`common.m4')ifelse(common(16), `ok', `',
6 `errprint(`Missing common initialization
9 # Easier to map tiles to valid macro name components:
13 define(`input', translit(include(defn(`file')), nl`./\|-', `012345'))
14 define(`x', 1)define(`y', 1)
15 define(`do', `ifelse(`$3', 0, `define(`maxx', $1)define(`x', 1)define(`y',
16 incr($2))', `define(`x', incr($1))define(`g$1_$2', $3)')')
19 patsubst(defn(`input'), `.', `do(x, y, \&)')
21 define(`chew', `ifelse($1, 1, `do(x, y, $2)', `$0(eval($1/2), substr(
22 `$2', 0, eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
23 chew(len(defn(`input')), defn(`input'))
26 define(`vr1', `visit(incr($1), $2, `r')')
27 define(`vu1', `visit($1, decr($2), `u')')
28 define(`vl1', `visit(decr($1), $2, `l')')
29 define(`vd1', `visit($1, incr($2), `d')')
31 define(`vr2', `visit($1, decr($2), `u')')
32 define(`vu2', `visit(incr($1), $2, `r')')
33 define(`vl2', `visit($1, incr($2), `d')')
34 define(`vd2', `visit(decr($1), $2, `l')')
36 define(`vr3', `visit($1, incr($2), `d')')
37 define(`vu3', `visit(decr($1), $2, `l')')
38 define(`vl3', `visit($1, decr($2), `u')')
39 define(`vd3', `visit(incr($1), $2, `r')')
41 define(`vr4', `visit($1, decr($2), `u')visit($1, incr($2), `d')')
42 define(`vu4', `visit($1, decr($2), `u')')
43 define(`vl4', `visit($1, decr($2), `u')visit($1, incr($2), `d')')
44 define(`vd4', `visit($1, incr($2), `d')')
46 define(`vr5', `visit(incr($1), $2, `r')')
47 define(`vu5', `visit(incr($1), $2, `r')visit(decr($1), $2, `l')')
48 define(`vl5', `visit(decr($1), $2, `l')')
49 define(`vd5', `visit(incr($1), $2, `r')visit(decr($1), $2, `l')')
51 define(`use', `define(`$1')pushdef(`all', `$1')')
52 define(`clean', `ifdef(`all', `popdef(defn(`all'))popdef(`all')$0()')')
53 define(`_visit', `ifdef(`e$1_$2', `', `.use(`e$1_$2')')use(
54 `e$1_$2_$3')v$3$4($1, $2)')
55 define(`visit', `ifdef(`g$1_$2', `ifdef(`e$1_$2_$3', `', `_$0($@, g$1_$2)')')')
56 define(`track', `len(visit($@))clean()')
57 define(`part1', track(1, 1, `r'))
59 define(`part2', part1)
60 define(`check', `ifelse(eval(part2 < $1), 1, `define(`part2', $1)')')
62 forloop(1, decr(maxx), `check(track(', `, 1, `d'))')
64 forloop(2, decr(y), `check(track(1, ', `, `r'))')
66 forloop(1, decr(maxx), `check(track(', `, 'decr(y)`, `u'))')
68 forloop(1, decr(y), `check(track('decr(maxx)`, ', `, `l'))')