1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dhashsize=H] [-Dfile=day17.input] day17.m4
4 include(`common.m4')ifelse(common(17, 1000003), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit((include(defn(`file'))), nl`= ,()', `;..'))
9 define(`miny', `99999')define(`maxy', `0')
10 define(`part1', 0)define(`part2', 0)
11 define(`point', `define(`g$1_$2', 1)')
12 define(`visit', `_$0(translit(`$1', `.', `,'))')
13 define(`_visit', `forloop(`$4', `$6', `point('ifelse(`$1', `x',
14 ``$2,',', `,`,$2'')`)')ifelse(`$1', `x', `ifelse(eval($6 > maxy), 1,
15 `define(`maxy', `$6')')ifelse(eval($4 < miny), 1, `define(`miny', `$4')')')')
17 patsubst(defn(`input'), `\([^;]*\);', `visit(`\1')')
19 define(`_chew', `visit(substr(`$1', 0, index(`$1', `;')))define(`tail',
20 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'), `;'), -1,
21 `', `$0(defn(`tail'))')')
22 define(`chew', `ifelse(eval(`$1 < 40'), 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(`input')), defn(`input'))
27 define(`bump', `define(`g$1_$2', 2)define(`part1', incr(part1))')
28 define(`flow', `ifelse(`$2', 'incr(maxy)`, `', `first(`$0'defn(
29 `g$1_$2')`_'defn(`g$1_$3'))($@)')')
30 define(`flow_', `bump(`$1', `$2')flow(`$1', `$3', incr(`$3'))')
31 define(`flow2_1', `first(`spill'fill(decr(`$1'), `$2', `$3', `decr',
32 `l')fill(incr(`$1'), `$2', `$3', `incr', `r'))(l, `$1', r, `$2', `$3')')
33 define(`flow2_3', defn(`flow2_1'))
34 define(`flow_1', `bump(`$1', `$2')flow2_1($@)')
35 define(`flow_2', `bump(`$1', `$2')')
36 define(`flow_3', defn(`flow_1'))
37 define(`fill', `first(`$0'defn(`g$1_$2')`_'defn(`g$1_$3'))($@)')
38 define(`fill2_1', `fill($4(`$1'), `$2', `$3', `$4', `$5')')
39 define(`fill2_3', defn(`fill2_1'))
40 define(`fill_1', `bump(`$1', `$2')fill2_1($@)')
41 define(`fill_3', defn(`fill_1'))
42 define(`fill1_', `define(`$5', `$1')1')
43 define(`fill1_1', defn(`fill1_'))
44 define(`fill1_2', defn(`fill1_'))
45 define(`fill1_3', defn(`fill1_'))
46 define(`fill_', `define(`$5', `$1')0')
47 define(`fill_2', defn(`fill_'))
48 define(`fill2_2', defn(`fill_'))
49 define(`spill00', `flow(`$1', `$4', `$5')flow(`$3', `$4', `$5')')
50 define(`spill01', `flow(`$1', `$4', `$5')')
51 define(`spill10', `flow(`$3', `$4', `$5')')
52 define(`spill11', `forloop(incr(`$1'), decr(`$3'), `define(`g'',
53 ``_$4', `3')define(`part2', incr(part2))')flow(`$2', decr(`$4'), `$4')')
54 flow(500, miny, incr(miny))