1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day21.input] day21.m4
4 include(`common.m4')ifelse(common(21), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(defn(`file')), nl, `;'))
9 define(`line', `_$0(translit(`$1', ` ', `,'))')
10 define(`_line', `pushdef(`inst', `$@')')
12 patsubst(defn(`list'), `\([^;]*\);', `line(`\1')')
14 define(`chew', `line(substr(`$1', 0, index(`$1', `;')))define(`tail',
15 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'), `;'), -1,
16 `', `$0(defn(`tail'))')')
17 define(`split', `ifelse(eval($1 < 75), 1, `chew(`$2')', `$0(eval($1/2),
18 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
19 defn(`tail')substr(`$2', eval($1/2)))')')
20 split(len(defn(`list')), defn(`list'))
22 define(`swapposition_', `swapletter_(`$1', `', `', substr(`$1', $4, 1), `', `',
23 substr(`$1', $7, 1))')
24 define(`swapletter_', `translit(`$1', `$4$7', `$7$4')')
25 define(`rotateleft_', `substr(`$1', $4)`'substr(`$1', 0, $4)')
26 define(`rotateright_', `rotateleft_(`$1', `', `', eval(8 - $4))')
27 define(`rotatebased_', `rotateright_(`$1', `', `', eval((5 * index(`$1',
29 define(`reversepositions_', `ifelse(eval($6 > $4), 1, `$0(swapposition_(`$1',
30 `', `', `$4', `', `', `$6'), `', `', incr($4), `', decr($6))', `$1')')
31 define(`moveposition_', `_$0(substr(`$1', 0, $4)`'substr(`$1', incr($4)),
32 substr(`$1', $4, 1), $7)')
33 define(`_moveposition_', `substr(`$1', 0, $3)`$2'substr(`$1', $3)')
35 define(`rswapposition_', `swapposition_($@)')
36 define(`rswapletter_', `swapletter_($@)')
37 define(`rrotateleft_', `rotateright_($@)')
38 define(`rrotateright_', `rotateleft_($@)')
39 define(`rrotatebased_', `rotateleft_(`$1', `', `', defn(`rrb'index(`$1', $8)))')
40 define(`rrb0', 1)define(`rrb1', 1)define(`rrb2', 6)define(`rrb3', 2)
41 define(`rrb4', 7)define(`rrb5', 3)define(`rrb6', 0)define(`rrb7', 4)
42 define(`rreversepositions_', `reversepositions_($@)')
43 define(`rmoveposition_', `moveposition_(`$1', `', `', $7, `', `', $4)')
45 define(`w', `fbgdceah')
46 define(`do', `define(`w', r$1$2_(w, $@))')
47 stack_reverse(`inst', `rev', `do(inst)')
50 define(`w', `abcdefgh')
51 define(`do', `define(`w', $1$2_(w, $@))')
52 stack_reverse(`rev', `inst', `do(inst)')