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
9 define(`input', translit(include(defn(`file')), Tsemantic))
10 define(`half', `_$0(mul(5, $1))')define(`_half', `substr($1, 0, decr(len($1)))')
11 define(`_search', `ifelse(, $@, lt64(mul64($4, eval($1 - $4)), $2), 0,
12 `search($1, $2, $3, $4)', `search($1, $2, $4, $5)')')
13 define(`search', `_$0($1, $2, $3, half(add($3, $4)), $4)')
14 define(`count', `eval($1-search($1, $2, 0, half($1))*2+1)')
15 define(`zip', `ifelse($1$2, ()(), , first$1,, `zip((shift$1),$2)', first$2,,
16 `zip($1,(shift$2))', `,count(first$1, first$2)zip((shift$1),(shift$2))')')
17 define(`part1', mul(1zip(translit(input, :D nl, (,,)))))
18 define(`part2', count(translit(input, nl :D, `,')))