1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day6.input] day6.m4
4 include(`common.m4')ifelse(common(6), `ok', `',
5 `errprint(`Missing common initialization
8 # The input files contain bare ), which is a pain to deal with in m4.
9 # We tackle this by reading input twice: on the first read, the ) in the
10 # input terminates our define() early, but we are diverting to -1, and
11 # the rest of the input is harmless. On the second read, we redefine
12 # comments to begin with the start of input and end with double newline
13 # (one in the file, one that we supply), thus reading the entire input as
14 # a single comment, at which point we can then post-process as desired
15 # into a saner form. At least the input does not also contain characters
16 # like [(`',] or macro names, which would have made this worse.
18 ifdef(`file', `', `errprint(`Missing definition of file
20 define(`input', include(defn(`file'))
21 changecom(defn(`input'), `
24 define(`input', include(defn(`file'))
27 define(`input', translit(dquote(defn(`input')), `)
30 define(`add', `ifelse($1, `', `', `_$0(translit($1, `.', `,'))')')
31 define(`_add', `pushdef(`nodes', `$2')define(`n$2', `$1')')
36 define(`loop', `ifdef(`nodes', `define(`part1',
37 eval(part1 + chase(defn(`nodes'))))popdef(`nodes')loop()')')
38 define(`chase', `ifdef(`d$1', `', `define(`d$1',
39 incr(chase(defn(`n$1'))))')d$1')
42 define(`part2', ``part2 not possible'')
43 define(`merge', `ifdef(`n$1', `ifdef(`m$1', `define(`part2',
44 eval(dYOU + dSAN - 2 - 2 * d$1))', `define(`m$1')merge(defn(`n$1'))')')')
45 ifelse(ifdef(`dYOU', 1).ifdef(`dSAN', 1), 1.1, `merge(`YOU')merge(`SAN')')