1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day06.input] day06.m4
4 include(`common.m4')ifelse(common(06), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit(include(defn(`file')), alpha`'nl, ALPHA))
9 define(`cnt', 1)define(`cur')
10 define(`goal', `ABCDEFGHIJKLMN')define(`need', 10)
11 define(`end', `define(`part2', `$1')pushdef(`do')')
12 pushdef(`goal', `ABCD')pushdef(`need', 4)
13 pushdef(`end', `define(`part1', `$1')popdef(`goal')popdef(`end')popdef(`need')')
14 define(`collect', `ifelse($2, 0, `substr(`$3', 1)', `define(`need',
15 decr($2))`$3'')`$1', defn(`goal')')
16 define(`cmp', `ifelse(translit(`$2', `$2', `$3'), `$3', `end(`$1')')define(
18 define(`do', `cmp($1, collect(`$2', need, defn(`cur')))define(`cnt', incr($1))')
21 patsubst(defn(`input'), `.', `do(cnt, `\&')')
23 define(`chew', `ifelse($1, 1, `do(cnt, `$2')', `$0(eval($1/2), substr(
24 `$2', 0, eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
25 chew(len(defn(`input')), defn(`input'))