1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day5.input] day5.m4
4 include(`common.m4')ifelse(common(5), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(file), `abcdefghijklmnopqrstuvwxyz'nl,
9 `ABCDEFGHIJKLMNOPQRSTUVWXYZ;'))
10 define(`part1', 0)define(`part2', 0)
11 define(`check', `ifelse(eval(len(translit(`$1', `BCDFGHJKLMNPQRSTVWXYZ')) >
12 2)index(`$1', `AB')index(`$1', `CD')index(`$1', `PQ')index(`$1', `XY'),
13 1-1-1-1-1, `double(`$1')')two(`$1')')
15 define(`double', `regexp(`$1', `\(.\)\1', `define(`part1', incr(part1))')')
16 define(`two', `regexp(`$1', `\(..\).*\1', `_$0(`$1')')')
17 define(`_two', `regexp(`$1', `\(.\).\1', `define(`part2', incr(part2))')')
18 patsubst(defn(`list'), `\([A-Z]*\);', `check(`\1')')
20 define(`double', `_$0(substr(`$1', 0, 1), substr(`$1', 1))')
21 define(`_double', `ifelse(`$2', `', `', index(`$2', `$1'), 0, `define(
22 `part1', incr(part1))', `$0(substr(`$2', 0, 1), substr(`$2', 1))')')
23 define(`two', `define(`a')define(`b')_$0(substr(`$1', 0, 1), substr(`$1',
24 1, 1), substr(`$1', 2))')
25 define(`_two', `ifelse(index(`$3', `$1'), 0, `define(`a', -)')ifelse(
26 index(`$3', `$1$2'), -1, `', `define(`b', -)')ifelse(`$3', `', `', a`'b,
27 --, `define(`part2', incr(part2))', `$0(`$2', substr(`$3', 0, 1),
29 define(`chew', `check(substr(`$1', 0, index(`$1', `;')))define(`tail',
30 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
31 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
32 define(`split', `ifelse(eval($1 < 40), 1, `chew(`$2')', `$0(eval($1/2),
33 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
34 defn(`tail')substr(`$2', eval($1/2)))')')
35 split(len(defn(`list')), defn(`list'))