1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day2.input] day2.m4
4 include(`common.m4')ifelse(common(2), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(defn(`file')), nl, `N'))
9 define(`part1')define(`part2')define(`k', 5)define(`K', 5)
10 define(`map', `define(`U$1', `$2')define(`R$1', `$3')define(`D$1',
11 `$4')define(`L$1', `$5')define(`U_$1', `$6')define(`R_$1', `$7')define(
12 `D_$1', `$8')define(`L_$1', `$9')')
13 map(1, 1, 2, 4, 1, 1, 1, 3, 1)
14 map(2, 2, 3, 5, 1, 2, 3, 6, 2)
15 map(3, 3, 3, 6, 2, 1, 4, 7, 2)
16 map(4, 1, 5, 7, 4, 4, 4, 8, 3)
17 map(5, 2, 6, 8, 4, 5, 6, 5, 5)
18 map(6, 3, 6, 9, 5, 2, 7, a, 5)
19 map(7, 4, 8, 7, 7, 3, 8, b, 6)
20 map(8, 5, 9, 8, 7, 4, 9, c, 7)
21 map(9, 6, 9, 9, 8, 9, 9, 9, 8)
22 map(a, , , , , 6, b, a, a)
23 map(b, , , , , 7, c, d, a)
24 map(c, , , , , 8, c, c, b)
25 map(d, , , , , b, d, d, d)
26 define(`visit', `ifelse(`$1', `N', `define(`part1', defn(`part1')k)define(
27 `part2', defn(`part2')translit(K, `abcd', `ABCD'))', `define(`k',
28 defn(`$1'k))define(`K', defn(`$1_'K))')')
30 patsubst(defn(`list'), `.', `visit(`\&')')
32 define(`split', `ifelse($1, 1, `visit(`$2')', `$0(eval($1/2), substr(`$2',
33 0, eval($1/2)))$0(eval($1 - $1/2), substr(`$2', eval($1/2)))')')
34 split(len(defn(`list')), defn(`list'))