1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day20.input] day20.m4
4 include(`common.m4')ifelse(common(20), `ok', `',
5 `errprint(`Missing common initialization
9 define(`list', translit(include(defn(`file')), `.#'nl, `01'))
10 define(`flip', `ifelse(`$1', `', `', `$0(substr(`$1', $2), $2)substr(`$1', 0,
12 define(`diag', `forloop_var(`i', 0, $2 - 1, `forloop(0, decr($2), `substr($1,
13 eval(', ` * $2 + i), 1)')')')
14 define(`tile', `_$0(translit(`$1', ` :', `,,'))')
15 define(`_tile', `pushdef(`tiles', `$2')define(`t$2', forloop(1, 8,
16 `substr(`$3', ', `1, 8)')findedges(`$3', `$2'))')
17 define(`edge', `ifdef(`e$1', `define(`e$1', defn(`e$1')`, $2')',
18 `pushdef(`edges', $1)define(`e$1', $2)')')
19 define(`edgepair', `edge($1, $2)edge(flip($1, 1), $2)`, $1'')
20 define(`findedges', `edgepair(substr(`$1', 0, 10), `$2')edgepair(substr(`$1',
21 90), `$2')edgepair(forloop(0, 9, `substr(`$1', ', `0, 1)'),
22 `$2')edgepair(forloop(0, 9, `substr(`$1', ', `9, 1)'), `$2')')
24 patsubst(defn(`list'), translit(eval(110, 1), 1, .), `tile(`\&')')
26 define(`half', `eval($1/110/2*110)')
27 define(`chew', `ifelse($1, 110, `tile(`$2')', `$0(half($1), substr(`$2', 0,
28 half($1)))$0(eval($1-half($1)), substr(`$2', half($1)))')')
29 chew(len(defn(`list')), defn(`list'))
31 define(`part1', 1)define(`part2', 0)
32 define(`count', `define(`part2', eval(part2 + len(translit($1, 0))))$@')
33 define(`_check', `ifelse(`$1', `$2', -)')
34 define(`check', `ifelse(_foreach(`_$0($1, defn(`e'', `))', count(t$1)), --,
35 `define(`part1', mul64(part1, $1))define(`p_0_0', $1)')')
36 stack_foreach(`tiles', `check')
37 define(`hflip', `define(`t$1', _$0(t$1))')
38 define(`_hflip', `forloop(0, 8, `flip(substr($1, eval(', ` * 8), 8),
39 1)')`, 'flip($2, 1)`, 'flip($3, 1)`, $5, $4'')
40 define(`vflip', `define(`t$1', _$0(t$1))')
41 define(`_vflip', `flip($1, 8)`, $3, $2, 'flip($4, 1)`, 'flip($5, 1)')
42 define(`swap', `define(`t$1', _$0(t$1))')
43 define(`_swap', `diag($1, 8)`, $4, $5, $2, $3'')
44 define(`right', `_$0(t$1)')
45 define(`_right', `$5')
46 define(`below', `_$0(t$1)')
47 define(`_below', `$3')
48 define(`neighbor', `_$0($2, first(defn(`e'$1($2))))')
49 define(`_neighbor', `ifelse($@, $2)')
50 define(`orient', `first(`orient'_$0($2, flip($2, 1), t$1)`$3($1)')')
51 define(`_orient', `ifelse($1, $4, 1, $1, $5, 2, $1, $6, 3, $1, $7, 4,
52 $2, $4, 5, $2, $5, 6, $2, $6, 7, 8)')
53 define(`orient1r', `swap($1)')
54 define(`orient2r', `swap($1)hflip($1)')
55 define(`orient3r', `')
56 define(`orient4r', `hflip($1)')
57 define(`orient5r', `swap($1)vflip($1)')
58 define(`orient6r', `swap($1)vflip($1)hflip($1)')
59 define(`orient7r', `vflip($1)')
60 define(`orient8r', `vflip($1)hflip($1)')
61 define(`orient1b', `')
62 define(`orient2b', `vflip($1)')
63 define(`orient3b', `swap($1)')
64 define(`orient4b', `swap($1)vflip($1)')
65 define(`orient5b', `hflip($1)')
66 define(`orient6b', `vflip($1)hflip($1)')
67 define(`orient7b', `swap($1)hflip($1)')
68 define(`orient8b', `swap($1)vflip($1)hflip($1)')
69 ifelse(defn(`e'right(p_0_0)), p_0_0, `hflip(p_0_0)')
70 ifelse(defn(`e'below(p_0_0)), p_0_0, `vflip(p_0_0)')
71 define(`x', 0)define(`y', 0)
72 define(`nextrow', `define(`x', 0)_$0(y, incr(y))')
73 define(`_nextrow', `define(`p_0_$2', neighbor(`below', p_0_$1))orient(p_0_$2,
74 below(p_0_$1), `b')define(`y', $2)')
75 pushdef(`nextrow', `popdef(`$0')')
76 define(`nextcol', `_$0(x, incr(x), y)')
77 define(`_nextcol', `define(`p_$2_$3', neighbor(`right', p_$1_$3))orient(
78 p_$2_$3, right(p_$1_$3), `r')define(`x', $2)')
79 define(`until', `ifelse($1, $2, `', `$3`'$0($@)')')
80 until(`neighbor(`below', defn(`p_'x`_'y))', `defn(`p_'x`_'y)', `nextrow()
81 until(`neighbor(`right', defn(`p_'x`_'y))', `defn(`p_'x`_'y)', `nextcol()')')
82 forloop_var(`j', 0, y, `forloop_var(`k', 0, 7, `define(`image'eval(j * 8 + k),
83 forloop_var(`i', 0, x, `substr(first(first(defn(`t'defn(`p_'i`_'j)))),
85 define(`hpatt', ``18,0', `0,1', `5,1', `6,1', `11,1', `12,1', `17,1', `18,1',
86 `19,1', `1,2', `4,2', `7,2', `10,2', `13,2', `16,2'')
87 define(`vpatt', ``0,18', `1,0', `1,5', `1,6', `1,11', `1,12', `1,17', `1,18',
88 `1,19', `2,1', `2,4', `2,7', `2,10', `2,13', `2,16'')
90 define(`match', `_$0(`$1, $2, $3, $4', $5)')
91 define(`_match', `ifelse(`$2', `', `define(`monsters', incr(monsters))',
92 `ifelse(at($1, $2), 1, `$0(`$1', shift(shift($@)))')')')
93 define(`at', `substr(defn(`image'eval($2 + ($4$6))), eval($1 + ($3$5)), 1)')
94 forloop_var(`j', 0, (y+1) * 8 - 3, `forloop(0, eval((x+1) * 8 - 20), `match(',
95 `, j, `', `', defn(`hpatt'))')')
96 forloop_var(`j', 0, (y+1) * 8 - 3, `forloop(0, eval((x+1) * 8 - 20), `match(',
97 `, j, `19-', `', defn(`hpatt'))')')
98 forloop_var(`j', 0, (y+1) * 8 - 3, `forloop(0, eval((x+1) * 8 - 20), `match(',
99 `, j, `', `2-', defn(`hpatt'))')')
100 forloop_var(`j', 0, (y+1) * 8 - 3, `forloop(0, eval((x+1) * 8 - 20), `match(',
101 `, j, `19-', `2-', defn(`hpatt'))')')
102 forloop_var(`j', 0, (y+1) * 8 - 20, `forloop(0, eval((x+1) * 8 - 3), `match(',
103 `, j, `', `', defn(`vpatt'))')')
104 forloop_var(`j', 0, (y+1) * 8 - 20, `forloop(0, eval((x+1) * 8 - 3), `match(',
105 `, j, `', `19-', defn(`vpatt'))')')
106 forloop_var(`j', 0, (y+1) * 8 - 20, `forloop(0, eval((x+1) * 8 - 3), `match(',
107 `, j, `2-', `', defn(`vpatt'))')')
108 forloop_var(`j', 0, (y+1) * 8 - 20, `forloop(0, eval((x+1) * 8 - 3), `match(',
109 `, j, `2-', `19-', defn(`vpatt'))')')
110 define(`part2', eval(part2 - 15 * monsters))