1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day18.input] day18.m4
4 include(`common.m4')ifelse(common(18), `ok', `',
5 `errprint(`Missing common initialization
8 define(`part1', 0)define(`part2', 0)define(`bound', 1)
9 define(`bump', `ifelse(eval($1>=bound), 1, `define(`bound', incr($1))')')
10 define(`_do', `pushdef(`pts', `$1,$2,$3')define(`p$1_$2_$3')define(`part1',
11 eval(part1+6))bump($1)bump($2)bump($3)')
12 define(`do', `_$0(translit(`$1', `.', `,')))')
14 define(`input', translit((include(defn(`file'))), nl`,()', `;.'))
16 patsubst(defn(`input'), `\([^;]*\);', `do(`\1')')
18 define(`_chew', `do(substr(`$1', 0, index(`$1', `;')))define(
19 `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'),
20 `;'), -1, `', `$0(defn(`tail'))')')
21 define(`chew', `ifelse(eval($1 < 17), 1, `_$0(`$2')', `$0(eval($1/2),
22 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
23 defn(`tail')substr(`$2', eval($1/2)))')')
24 chew(len(defn(`input')), defn(`input'))
27 # Part 1: Identify all pairs of points that share a common face.
28 define(`shares', `ifdef(`p$1_$2_$3', `define(`part1', decr(decr(part1)))')')
29 define(`_check', `shares(incr($1), $2, $3)shares($1, incr($2), $3)shares($1,
31 define(`check', `ifdef(`pts', `_$0(pts)popdef(`pts')$0()')')
34 # Part 2: Identify all faces of points reachable in a modulo bounding box.
35 define(`fill', `ifdef(`p$1_$2_$3', `define(`part2', incr(part2))',
36 `ifdef(`v$1_$2_$3', `', `define(`v$1_$2_$3')$0(eval(`($1+1)%$4'), $2, $3,
37 $4)$0(eval(`($1+$4-1)%$4'), $2, $3, $4)$0($1, eval(`($2+1)%$4'), $3,
38 $4)$0($1, eval(`($2+$4-1)%$4'), $3, $4)$0($1, $2, eval(`($3+1)%$4'),
39 $4)$0($1, $2, eval(`($3+$4-1)%$4'), $4)')')')
40 fill(bound, bound, bound, incr(bound))