day 23 support more inputs
[aoc_eblake.git] / 2021 / day13.m4
blob1e909a43eacdd3c7a003a98c3bb31df6e149c317
1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day13.input] day13.m4
4 include(`common.m4')ifelse(common(13), `ok', `',
5 `errprint(`Missing common initialization
6 ')m4exit(1)')
8 define(`input', translit((include(defn(`file'))), nl`,fold along()', `;.'))
9 define(`offset', incr(index(defn(`input'), `;;')))
10 define(`points', substr(defn(`input'), 0, offset))
11 define(`folds', substr(defn(`input'), incr(offset)))
12 define(`count', 0)
13 define(`point', `define(`count', incr(count))define(`p$1_$2')pushdef(`p0',
14   `$1,$2')')
15 ifdef(`__gnu__', `
16   patsubst(defn(`points'), `\([^.]*\)\.\([^;]*\);', `point(`\1', `\2')')
17 ', `
18   define(`visit', `point(translit($1, `.', `,'))')
19   define(`_chew', `visit(substr(`$1', 0, index(`$1', `;')))define(
20     `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'),
21     `;'), -1, `', `$0(defn(`tail'))')')
22   define(`chew', `ifelse(eval($1 < 20), 1, `_$0(`$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   chew(len(defn(`points')), defn(`points'))
27 define(`swap', `popdef(`p$1_$2')ifdef(`p$3_$4', `define(`count', decr(count))',
28   `define(`p$3_$4')')`$3,$4'')
29 define(`foldx', `ifelse(eval($1 > $3), 1, `swap($1, $2, eval(2*$3-$1), $2)',
30   ``$1,$2'')')
31 define(`foldy', `ifelse(eval($2 > $3), 1, `swap($1, $2, $1, eval(2*$3-$2))',
32   ``$1,$2'')')
33 define(`fold', `ifdef(`p$1', `pushdef(`p$2', $0$3(p$1, $4))popdef(
34   `p$1')$0($@)', `define(`last$3', $4)')')
35 define(`hook', `define(`part1', count)pushdef(`hook')')
36 define(`do', `ifelse(`$2', `', `', `fold($1, incr($1), `$2', $3)hook()$0(
37   incr($1), shift(shift(shift($@))))')')
38 do(0, translit(defn(`folds'), `=;', `,,'))
39 include(`ocr.m4')
40 define(`char', `forloop(0, 5, `_$0(', `, $1)')')
41 define(`_char', `forloop($2, eval($2+4), `ifdef(`p'', ``_$1', ``X'', `` '')')')
42 define(`part2', forloop(0, 7, `ocr(char(eval(', `*5)))'))
44 divert`'part1
45 part2