1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day19.input] [-Dhashsize=H] day19.m4
4 include(`common.m4')ifelse(common(19, 65537), `ok', `',
5 `errprint(`Missing common initialization
8 define(`part1', 0)define(`max', 0)
9 define(`input', translit(include(defn(`file')), `e'nl, `E;'))
10 define(`list', substr(defn(`input'), 0, incr(index(defn(`input'), `;;'))))
11 define(`mol', translit(substr(defn(`input'), index(defn(`input'), `;;')), `;'))
12 define(`rev', `ifelse(`$1', `', `', `$0(substr(`$1', 1))`'substr(`$1', 0, 1)')')
13 define(`line', `pushdef(`$1_', `$2')pushdef(`out', rev(`$2'))define(defn(
14 `out')`_', rev(`$1'))ifelse(eval(len(`$2') > max), 1, `define(`max',
17 patsubst(defn(`list'), `\([^ ]*\) => \([^ ]*\);', `line(`\1', `\2')')
19 define(`chew', `line(translit(substr(`$1', 0, index(`$1', `;')), `=> ',
20 `,'))define(`tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
21 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
22 define(`split', `ifelse(eval($1 < 40), 1, `chew(`$2')', `$0(eval($1/2),
23 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
24 defn(`tail')substr(`$2', eval($1/2)))')')
25 split(len(defn(`list')), defn(`list'))
27 define(`res', `ifdef(`$1$2$3', `', `define(`$1$2$3')define(`part1',
29 define(`visit', `ifelse(`$2', `', `', index('alpha`, substr(`$2', 1, 1)), -1,
30 `_$0(`$1', substr(`$2', 0, 1), substr(`$2', 1))', `_$0(`$1', substr(`$2', 0,
31 2), substr(`$2', 2))')')
32 define(`_visit', `_stack_foreach(`$2_', `res(`$1',', `, `$3')',
33 `t')visit(`$1$2', `$3')')
36 define(`mol0', rev(mol))
37 define(`check', `ifelse(eval($1 >= 0 && $1 <= best), 1, `define(`best',
38 $1)define(`mol$3', substr(`$4', 0, $1)`'defn(`$2_')substr(`$4',
39 eval($1 + len(`$2'))))')')
40 define(`contract', `_stack_foreach(`out', `_$0(', `, $@)', `t')')
41 define(`_contract', `check(index(`$3', `$1'), $@)')
42 define(`round', `define(`best', len(mol$1))ifelse(best, 1, `define(`part2',
43 $1)', `contract(incr($1), mol$1)$0(incr($1))')')