1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Ddelay=N] [-Dworkers=N] [-Dfile=day7.input] day7.m4
4 include(`common.m4')ifelse(common(7), `ok', `',
5 `errprint(`Missing common initialization
8 ifdef(`delay', `', `define(`delay', 60)')
9 ifdef(`workers', `', `define(`workers', 5)')
11 define(`prep', `define(`l$1', substr('ALPHA`, `$1', 1))define(`d'l$1,
13 forloop_arg(0, 25, `prep')
15 define(`add', `define(`pre$8', defn(`pre$8')`$2')ifelse(eval(d$8 >
16 defn(`d'last)), 1, `define(`last', `$8')')')
17 translit(include(defn(`file')), ` ', `,'define(`Step',
18 `add(')define(`can', `)'))
19 define(`steps', eval(defn(`d'last) - delay))
20 define(`find', `define(`part1', `$1'_$0(1, l0, `$1'))')
21 define(`_find', `ifelse(`$1', 'incr(steps)`, `', translit(translit(`$2',
22 defn(`q'))`'defn(`pre$2'), `$3'), `$2', ``$2'', `$0(incr(`$1'),
24 forloop(0, decr(steps), `find(defn(`part1'),', `)')
25 forloop(1, workers, `define(`w'', ``t', 0)')
26 define(`_next', `ifelse(eval(`$2 && $2 < $1'), 1, `$2', `$1')')
27 define(`next', `ifelse($2, 'incr(workers)`, `$1', `$0(ifelse($1, 0, `w$2t',
28 `_$0(`$1', w$2t)'), incr($2))')')
29 define(`do', `define(`q')forloop(1, 'workers`, `$0_1(', `, `$1')')ifelse(
30 len(defn(`done')), 'steps`, `$1', `forloop(1, 'workers`, `$0_2(',
31 `, 'defn(`done')`, defn(`q'), `$1')')$0(next(0, 1))')')
32 define(`do_1', `ifelse(w$1t, `$2', `define(`done', defn(`done')defn(
33 `w$1l'))define(`w$1t', 0)define(`w$1l')', `define(`q',
34 defn(`q')defn(`w$1l'))')')
35 define(`do_2', `ifelse(w$1t, 0, `define(`w$1l', _find(1, l0, `$2'))ifelse(w$1l,
36 `', `', `define(`q', defn(`q')defn(`w$1l'))define(`w$1t',
37 eval($4 + defn(`d'w$1l)))')')')
38 define(`part2', do(0))