1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day22.input] day22.m4
4 include(`common.m4')ifelse(common(22), `ok', `',
5 `errprint(`Missing common initialization
8 define(`list', translit(include(defn(`file')), nl`#', `;'))
9 define(`list', substr(defn(`list'), eval(5 + index(defn(`list'), `Use%;'))))
11 define(`oops', `errprintn(`data different than assumptions')m4exit(1)')
12 define(`line', `define(`u'cnt, $1)define(`a'cnt, $2)ifelse($1, 0, `ifdef(`z',
13 `oops', `define(`z', cnt)')', len($1), 3, `ifdef(`f', `ifdef(`Y',
14 `ifelse(eval((cnt - f) % Y), 0, `', `oops')', `define(`Y', eval(cnt - f))')',
15 `ifdef(`z', `oops')define(`f', cnt)')')define(`cnt', incr(cnt))')
17 define(`d', ` *\([0-9][0-9]*\)')
18 patsubst(defn(`list'), `[^;]*'d`T'd`T'd`T'd`%;', `line(\2, \3)')
20 define(`prep', `line(substr(`$1', 29, 4), substr(`$1', 36, 4))')
21 define(`half', `eval($1/48/2*48)')
22 define(`chew', `ifelse($1, 48, `prep(`$2')', `$0(half($1), substr(`$2', 0,
23 half($1)))$0(eval($1-half($1)), substr(`$2', half($1)))')')
24 chew(len(defn(`list')), defn(`list'))
26 define(`X', eval(cnt / Y))
28 # Per part 2, we only have one empty enough node, so we really only have
29 # to count other nodes that fit, rather than checking every pair
30 define(`do', `eval(u$1 && u$1 < defn(`a'z))')
31 define(`part1', len(translit(forloop_arg(0, decr(cnt), `do'), 0)))
32 # We asserted above that the output resembles:
37 # Solve by starting at z, moving west past f, north to 0, east to G, then
39 define(`part2', eval((z/Y - f/Y + 1) + z%Y + (X - f/Y) + (5 * (X - 2))))