day 15 part 1 optimize
[aoc_eblake.git] / 2023 / day06.m4
blobae5f719c222bfb8c8a206bc9812e7e2161a618e8
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
6 ')m4exit(1)')
8 include(`math64.m4')
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, `,')))
20 divert`'part1
21 part2