1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day8.input] day8.m4
4 include(`common.m4')ifelse(common(8), `ok', `',
5 `errprint(`Missing common initialization
8 define(`input', translit(include(defn(`file')), `
11 define(`size', eval(25 * 6))
12 define(`zeroes', size)
13 forloop(0, decr(size), `define(`i'', `, 2)')
14 define(`layer', `_$0(substr(input, eval($1 * size), size))')
15 define(`_layer', `define(`c0', 0)define(`c1', 0)define(`c2', 0)define(`l',
16 $1)forloop_arg(0, size - 1, `pixel')ifelse(eval(c0 < zeroes), 1,
17 `define(`zeroes', c0)define(`part1', eval(c1 * c2))')')
18 define(`char', `define(`c$1', incr(c$1))')
19 define(`pixel', `_$0($1, substr(l, $1, 1))')
20 define(`_pixel', `char($2)ifelse(i$1, 2, `define(`i$1', $2)')')
21 forloop_arg(0, len(input) / size - 1, `layer')
24 define(`let', `forloop(0, 5, `_$0($1, ', `)')')
25 define(`_let', `forloop($1, eval($1+4), `ifelse(defn(`i'eval($2 * 25 + ',
26 `)), 1, ``X'', `` '')')')
27 define(`part2', forloop(0, 4, `ocr(let(eval(', `*5)))'))
29 define(`show', `ifelse(defn(`i'eval(y * 25 + $1)), 1, `X', ` ')')