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(`reg', 0)define(`p1', 0)define(`p2', -1)
9 define(`part1', 0)define(`part2', 0)
10 define(`list', translit(include(defn(`file')), alpha, ALPHA)nl)
11 define(`letter', `define(substr(ALPHA, decr($1), 1)`_',
12 `undefine(`eol')define(`reg', eval(reg | (1 << $1)))')')
13 forloop_arg(1, 26, `letter')
14 define(`pop', `len(translit(eval($1, 2), 0))')
15 define(`_', `ifdef(`eol',
16 `define(`part1', eval(part1 + pop(p1)))define(`part2',
17 eval(part2 + pop(p2)))define(`p1', 0)define(`p2', -1)',
18 `define(`eol')define(`p1', eval(p1 | reg))define(`p2',
19 eval(p2 & reg))define(`reg', 0)')')
21 `patsubst(defn(`list'), .\|nl, `\&_()')',
22 `define(`split', `ifelse($1, 1, `$2_()', `$0(eval($1/2), substr(`$2', 0,
23 eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
24 split(len(defn(`list')), defn(`list'))')