day 23 support more inputs
[aoc_eblake.git] / 2023 / day10.m4
blob1ef0e68e1ed7d0b09070be294e92f89a2b0d9889
1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day10.input] day10.m4
3 # Optionally with -Dcsv to enable CSV output
5 include(`common.m4')ifelse(common(10), `ok', `',
6 `errprint(`Missing common initialization
7 ')m4exit(1)')
9 # Remap into characters usable in macros.
10 # -|LJFS7.\n
11 # 123456708
13 # Another potential optimization not considered here:
14 # https://www.reddit.com/r/adventofcode/comments/18evyu9/comment/kcqmhwk/
15 # Use the shoelace formula to calculate area (determinate at each LJF7),
16 # then Pick's formula to determine interior points
18 define(`input', translit(include(defn(`file')), `-|LJFS.'nl, `12345608'))
19 define(`x', 1)define(`y', 1)
20 define(`p0')
21 define(`p1', `define(`p$3e$4', `incr($$1),$$2,`e'')define(`p$3w$4',
22   `decr($$1),$$2,`w'')')
23 define(`p2', `define(`p$3n$4', `$$1,decr($$2),`n'')define(`p$3s$4',
24   `$$1,incr($$2),`s'')define(`d$3_$4')')
25 define(`p3', `define(`p$3s$4', `incr($$1),$$2,`e'')define(`p$3w$4',
26   `$$1,decr($$2),`n'')define(`d$3_$4')')
27 define(`p4', `define(`p$3s$4', `decr($$1),$$2,`w'')define(`p$3e$4',
28   `$$1,decr($$2),`n'')define(`d$3_$4')')
29 define(`p5', `define(`p$3n$4', `incr($$1),$$2,`e'')define(`p$3w$4',
30   `$$1,incr($$2),`s'')')
31 define(`p6', `ifdef(`p$3n'decr($4), `define(`d$3_$4')define(`start',
32   `$3,'decr($4)`,`n'')', `ifdef(`p'decr($3)`w$4', `define(`start',
33   decr($3)`,$4,`w'')', `define(`start', incr($3)`,$4,`e'')')')define(
34   `p$3n$4')define(`p$3e$4')define(`p$3s$4')define(`p$3w$4')')
35 define(`p7', `define(`p$3n$4', `decr($$1),$$2,`w'')define(`p$3e$4',
36   `$$1,incr($$2),`s'')')
37 define(`p8', `define(`maxx', `$3')define(`x', 1)define(`y', incr($4))')
38 define(`do', `define(`x', incr($1))p$3(1, 2, $@)')
40 ifdef(`__gnu__', `
41   patsubst(defn(`input'), `.', `do(x, y, `\&')')
42 ',`
43   define(`chew', `ifelse($1, 1, `do(x, y, `$2')', `$0(eval($1/2), substr(
44     `$2', 0, eval($1/2)))$0(eval($1-$1/2), substr(`$2', eval($1/2)))')')
45   chew(len(defn(`input')), defn(`input'))
48 define(`run', `ifelse(`$2', `', `eval($1/2)',
49   `define(`v$2_$3', ``$4'')$0(incr($1), p$2$4$3($2, $3))')')
50 define(`part1', run(0, start))
52 define(`dump', `ifdef(`csv',
53   `divert(1)ifelse(`$1', nl, `', `,')$1$2`'divert(-1)', `$2')')
54 define(`_scan', `ifdef(`v$1_$2', `dump(v$1_$2)ifdef(`d$1_$2', `ifdef(`i',
55   `popdef(`i')', `define(`i', `1')')')', `dump(`', defn(`i'))')')
56 define(`scan', `forloop(1, 'decr(maxx)`, `_$0(', `,$1)')dump(nl)')
57 define(`collect', `forloop_arg(1, decr(y), `scan')')
58 ifdef(`csv', `collect`'define(`part2', ``=COUNT(a3:ez''incr(y)``)'')',
59   `define(`part2', len(collect))')
61 divert`'part1
62 part2