day 4 golf again
[aoc_eblake.git] / 2022 / day06.m4
blobc46a5fb6330e2e6f72bcc67091d4fd3e7d465eec
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 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(
17   `cur', `$2')')
18 define(`do', `cmp($1, collect(`$2', need, defn(`cur')))define(`cnt', incr($1))')
20 ifdef(`__gnu__', `
21   patsubst(defn(`input'), `.', `do(cnt, `\&')')
22 ',`
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'))
28 divert`'part1
29 part2