1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dhashsize=H] [-Dfile=day8.input] day8.m4
4 include(`common.m4')ifelse(common(8, 65537), `ok', `',
5 `errprint(`Missing common initialization
8 define(`inc', +)define(`dec', -)
10 define(`input', translit(include(defn(`file')), nl, `;'))
11 define(`get', `ifdef(`r_$1', `r_$1', 0)')
12 define(`set', `ifdef(`r_$1', `', `pushdef(`r_', `$1')')define(`r_$1',
13 eval(defn(`r_$1') + 0 $2 $3))ifelse(eval(r_$1 > part2), 1,
14 `define(`part2', r_$1)')')
15 define(`line', `_$0(translit(`$1', ` ', `,'))')
16 define(`_line', `ifelse(eval(get(`$5') $6 $7), 1, `set(`$1', $2, $3)')')
18 patsubst(defn(`input'), `\([^;]*\);', `line(`\1')')
20 define(`chew', `line(substr(`$1', 0, index(`$1', `;')))define(
21 `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
22 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
23 define(`split', `ifelse(eval($1 < 60), 1, `chew(`$2')', `$0(eval($1/2),
24 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
25 defn(`tail')substr(`$2', eval($1/2)))')')
26 split(len(defn(`input')), defn(`input'))
29 define(`find', `ifelse(eval(r_$1 > part1), 1, `define(`part1', r_$1)')')
30 _stack_foreach(`r_', `find(', `)', `tmp')