1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day07.input] day07.m4
4 include(`common.m4')ifelse(common(07), `ok', `',
5 `errprint(`Missing common initialization
8 # Input has no vowels other than in 'dir' and no mixed case; changing 'n' to
9 # 'N' is sufficient to avoid a collision with dnl, and no other macro collides
10 define(`input', translit(include(defn(`file')), nl`n', `;N'))
12 define(`push', `pushdef(`accum', 0)')
13 define(`_pop', `ifdef(`accum', `define(`accum', eval(accum + $1))')')
14 define(`pop', `ifelse(eval(accum < 100000), 1, `define(`part1',
15 eval(part1 + accum))')pushdef(`out', accum)_$0(accum`'popdef(`accum'))')
16 define(`_do', `ifelse(`$1$2', `$ls', `', `$1$3', `$..', `pop()',
17 `$1', `$', `push()', `$1', `dir', `', `define(`accum', eval(accum + $1))')')
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(`tail'),
25 `;'), -1, `', `$0(defn(`tail'))')')
26 define(`chew', `ifelse(eval($1 < 40), 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(`finish', `ifdef(`accum', `pop()finish()')')
33 finish()define(`part2', out)
34 define(`limit', eval(part2 - 40000000))
35 define(`finish', `ifdef(`out', `ifelse(eval(out < part2 && out > ''limit``),
36 1, `define(`part2', out)')popdef(`out')finish()')')