day 25 optimize and improve heuristics
[aoc_eblake.git] / 2017 / day1.m4
blobc4250f4eac75da8344d6b4fc434f9fcd81c41692
1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day1.input] day1.m4
4 include(`common.m4')ifelse(common(1), `ok', `',
5 `errprint(`Missing common initialization
6 ')m4exit(1)')
8 define(`input', translit(include(defn(`file')), nl))
9 define(`wrap', `substr(`$1', $2)substr(`$1', 0, $2)')
10 ifdef(`__gnu__', `
11   define(`get', `patsubst(`$1', `.', `pushdef(`$2', \&)')')
12 ',`
13   define(`split', `ifelse($1, 1, `pushdef(`$3', `$2')', `$0(eval($1/2),
14     substr(`$2', 0, eval($1/2)), `$3')$0(eval($1 - $1/2), substr(`$2',
15     eval($1/2)), `$3')')')
16   define(`get', `split(len(`$1'), `$1', `$2')')
18 get(defn(`input'), `a')
19 get(wrap(defn(`input'), 1), `b')
20 get(defn(`input'), `c')
21 get(wrap(defn(`input'), eval(len(defn(`input'))/2)), `d')
22 define(`compare', `eval(_$0($@))')
23 define(`_compare', `ifdef(`$1', `ifelse($1, $2, +$1)popdef(`$1')popdef(
24   `$2')$0($@)')')
25 define(`part1', compare(`a', `b'))
26 define(`part2', compare(`c', `d'))
28 divert`'part1
29 part2