2021: consistent sorting of file names
[aoc_eblake.git] / 2021 / day01.m4
blob9c033f17b05544ad45b37767b9e73da7eb7476b0
1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day01.input] day01.m4
4 include(`common.m4')ifelse(common(01), `ok', `',
5 `errprint(`Missing common initialization
6 ')m4exit(1)')
8 define(`part1', 0)define(`part2', 0)
9 define(`input', translit(include(defn(`file')), nl, `;'))
10 ifdef(`__gnu__', `
11   patsubst(defn(`input'), `\([^;]*\);', `pushdef(`v', `\1')')
12 ', `
13   define(`_chew', `pushdef(`v', substr(`$1', 0, index(`$1', `;')))define(
14     `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'),
15     `;'), -1, `', `$0(defn(`tail'))')')
16   define(`chew', `ifelse(eval($1 < 10), 1, `_$0(`$2')', `$0(eval($1/2),
17     substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
18     defn(`tail')substr(`$2', eval($1/2)))')')
19   chew(len(defn(`input')), defn(`input'))
21 define(`bumpif', `ifelse(eval($1 > $2), 1, `define(`$3', incr($3))')')
22 define(`do', `bumpif($1, $2, `part1')bumpif($2, $3, `part1')_$0($@,
23   popdef(`v')v)')
24 define(`_do', `bumpif($3, $4, `part1')bumpif($1, $4, `part2')popdef(
25   `v')ifdef(`v', `$0($2, $3, $4, v)')')
26 do(v, popdef(`v')v, popdef(`v')v)
28 ifelse(`dnl golfing variants of parts 1, 2:
29 eval(define(m,`ifelse($2,,,+($2>$1)`m(shift($@))')')m(translit(include(f),`'
30 ,`,')))
31 eval(define(m,`ifelse($4,,,+($4>$1)`m(shift($@))')')m(translit(include(f),`'
32 ,`,')))
35 divert`'part1
36 part2