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
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)))
13 define(`point', `define(`count', incr(count))define(`p$1_$2')pushdef(`p0',
16 patsubst(defn(`points'), `\([^.]*\)\.\([^;]*\);', `point(`\1', `\2')')
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)',
31 define(`foldy', `ifelse(eval($2 > $3), 1, `swap($1, $2, $1, eval(2*$3-$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'), `=;', `,,'))
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)))'))