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 # Lots of duplicate rows in input; track pairs seen for less work
9 define(`input', translit((include(defn(`file'))), nl`,>() -', `;..'))
11 define(`towards', `eval($1+($2>$1)-($1>$2))')
12 define(`_mark', `define(`p$1_$2')ifelse(`$1.$2', `$3.$4', `', `$0(towards(`$1',
13 `$3'), towards(`$2', `$4'), `$3', `$4')')')
14 define(`mark', `ifdef(`p$1_$2_$3_$4', `', `define(`p$1_$2_$3_$4')_$0(
15 $@)ifelse(eval($2 > maxy), 1, `define(`maxy', $2)')')')
16 define(`_do', `ifelse(`$3', `', `', `mark(`$1', `$2', `$3',
17 `$4')$0(shift(shift($@)))')')
18 define(`do', `_$0(translit(`$1', `.', `,'))')
21 patsubst(defn(`input'), `\([^;]*\);', `do(`\1')')
23 define(`_chew', `do(substr(`$1', 0, index(`$1', `;')))define(
24 `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
25 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
26 define(`chew', `ifelse(eval($1 < 600), 1, `_$0(`$2')', `$0(eval($1/2),
27 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
28 defn(`tail')substr(`$2', eval($1/2)))')')
29 chew(len(defn(`input')), defn(`input'))
32 define(`floor', incr(incr(maxy)))define(`count', 0)
33 define(`check', `ifdef(`p$1_$2', `', `find($1, $2, incr($2))')')
34 define(`find', `ifelse($2, 'maxy`, `ifdef(`part1', `', `define(`part1',
35 count)')')ifelse($2, 'floor`, `', `check($1, $3)check(decr($1), $3)check(
36 incr($1), $3)define(`p$1_$2')define(`count', incr(count))')')
37 find(500, 0, 1)define(`part2', count)