1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day11.input] [-Dhashsize=H] day11.m4
4 include(`common.m4')ifelse(common(11, 65537), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(defn(`file')), `.'nl, `FR'))
9 define(`x', 1)define(`y', 1)
10 define(`maxx', incr(index(list, `R')))
11 define(`maxy', incr(len(translit(list, `FL'))))
12 define(`coord', `eval($1 + $2 * 'maxx`)')
13 define(`F_', `define(`x', incr(x))')
14 define(`L_', `pushdef(`seat', coord(x, y))define(`s_'coord(x, y))F_()')
15 define(`R_', `define(`x', 1)define(`y', incr(y))')
17 patsubst(defn(`list'), `.', `\&_()')
19 define(`split', `ifelse($1, 1, `$2_()', `$0(eval($1/2), substr(`$2', 0,
20 eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
21 split(len(defn(`list')), defn(`list'))
25 # 1 - empty, but still live
26 # 2 - empty and locked (at least one neighbor in state 3)
27 # 3 - occupied and locked (less than four neighbors in state 1, 3, 4)
28 # 4 - occupied, but still live
30 define(`_s', `s(eval($1 $2), `$2')')
31 define(`find', `define(`s_$1', 1)define(`n_$1', _s($1, -$2-1)_s($1, -$2)_s($1,
32 -$2+1)_s($1, -1)_s($1, +1)_s($1, +$2-1)_s($1, +$2)_s($1, +$2+1))')
33 define(`check', `ifelse(_$0($1, s_$1), -, ``s'', $2)')
34 define(`_check', `check$2($1, n_$1)')
37 define(`check1', `ifelse(translit($2, 12), `', `pushdef(`act', `define(`s_$1',
38 eval(3 + (len(translit($2, 2)) >= 4)))')', index(translit($2, 124), 3), 0,
39 `pushdef(`act', `define(`s_$1', 2)')')')
40 define(`check4', `ifelse(index(translit($2, 3412, --), full), 0,
41 `pushdef(`act', `define(`s_$1', 1)')', eval(len(translit($2, 2)) < len(
42 full)), 1, `pushdef(`act', `define(`s_$1', 3)')')')
43 define(`visit', `ifdef(`$1', `pushdef(`seat'check($1, $2),
44 $1)popdef(`$1')visit($@)')')
45 define(`do', `ifdef(`act', `act`'popdef(`act')do()')')
46 define(`round', `visit(`seat$2', incr($2))ifdef(`act', `do()round($1,
47 incr($2))', `incr($2)')')
48 define(`compute', `stack_reverse(`seat', `seat0', `find(seat, 'maxx`)')define(
49 `full', `$2')define(`part$1', len(translit(stack_reverse(`seat'round($1, 0),
50 `seat', `defn(`s_'seat)')stack_reverse(`seats', `seat', `defn(`s_'seat)'),
53 define(`s', `ifdef(`s_$1', ``s_$1`''')')
55 define(`s', `ifelse(eval($1%'maxx` == 0 || ($1/'maxx`)%'maxy` == 0), 1, `',
56 `ifdef(`s_$1', ``s_$1`''', `s(eval($1 $2), `$2')')')')