1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day6.input] day6.m4
4 include(`common.m4')ifelse(common(6), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(defn(`file')), alpha`'nl, ALPHA`_'))
9 define(`bump', `define(`$1', incr(0defn(`$1')))')
11 define(`visit', `ifelse(`$1', `_', `define(`n', 0)', `bump(`$1'n)bump(`n')')')
13 patsubst(defn(`list'), `.', `visit(`\&')')
15 define(`split', `ifelse($1, 1, `visit(`$2')', `$0(eval($1/2), substr(`$2',
16 0, eval($1/2)))$0(eval($1 - $1/2), substr(`$2', eval($1/2)))')')
17 split(len(defn(`list')), defn(`list'))
19 define(`n', index(defn(`list'), `_'))
20 define(`find', `define(`mv', 0)forloop(0, 25, `_$0(substr(ALPHA, ', `, 1),
22 define(`_find', `ifelse(eval(defn(`$1$2') + 0 > mv), 1, `define(`ml',
23 `$1')define(`mv', $1$2)')')
24 define(`part1', translit(forloop_arg(0, decr(n), `find'), ALPHA, alpha))
25 define(`find', `define(`mv', 1000)forloop(0, 25, `_$0(substr(ALPHA, ', `, 1),
27 define(`_find', `ifdef(`$1$2', `ifelse(eval($1$2 < mv), 1, `define(`ml',
28 `$1')define(`mv', $1$2)')')')
29 define(`part2', translit(forloop_arg(0, decr(n), `find'), ALPHA, alpha))