1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day4.input] day4.m4
4 include(`common.m4')ifelse(common(4), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit(include(defn(`file')), alpha`'nl, ALPHA`;'))
9 define(`part1', 0)define(`part2', 0)
10 define(`check', `ifelse(`$1', `', `-', index(`$*,', `,$1,'), -1,
12 define(`arrange', `_$0(`$1')`'ifelse(`$2', `', `', `,$0(shift($@))')')
13 define(`_arrange', `(translit(defn(`p'len(`$1')),
16 define(`p2', `sort(`0',`1')')
17 define(`p3', `sort(`0'sort(`1',`2'))')
18 define(`p4', `sort(`0'sort(`1'sort(`2',`3')))')
19 define(`p5', `sort(`0'sort(`1'sort(`2'sort(`3', `4'))))')
20 define(`p6', `sort(`0'sort(`1'sort(`2'sort(`3'sort(`4',`5')))))')
21 define(`p7', `sort(`0'sort(`1'sort(`2'sort(`3'sort(`4'sort(`5',`6'))))))')
23 `sort(`0'sort(`1'sort(`2'sort(`3'sort(`4'sort(`5'sort(`6',`7')))))))')
24 define(`_prep', `define(`sort'substr('dquote(ALPHA)`, `$1', 1)`'substr(
25 'dquote(ALPHA)`, `$2', 1), eval(`$1 <= $2'))')
26 define(`prep', `forloop(0, 25, `_$0(`$1', ', `)')')
27 forloop_arg(0, 25, `prep')
28 define(`sort', `ifelse(`$2', `', `,`$1'', `ifelse(sort$1$2, 1, `,$@',
29 `,`$2'$0(`$1', shift(shift($@)))')')')
30 define(`line', `_$0(translit(`$1', ` ', `,'))')
31 define(`_line', `ifelse(check($@), -, `define(`part1', incr(part1))ifelse(
32 check(arrange($@)), -, `define(`part2', incr(part2))')')')
34 patsubst(defn(`input'), `\([^;]*\);', `line(`\1')')
36 define(`chew', `line(substr(`$1', 0, index(`$1', `;')))define(`tail',
37 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
38 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
39 define(`split', `ifelse(eval($1 < 180), 1, `chew(`$2')', `$0(eval($1/2),
40 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
41 defn(`tail')substr(`$2', eval($1/2)))')')
42 split(len(defn(`input')), defn(`input'))