1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day04.input] day04.m4
4 include(`common.m4')ifelse(common(04), `ok', `',
5 `errprint(`Missing common initialization
8 define(`part1', 0)define(`part2', 0)
9 define(`bump', `define(`$1', eval($1`+($2)'))')
10 define(`_do', `bump(`part1', `$1>=$3&$2<=$4|$3>=$1&$4<=$2')bump(`part2',
12 define(`do', `_$0(translit(`$1', `-', `,'))')
14 define(`input', translit((include(defn(`file'))), nl`,()', `;-'))
16 patsubst(defn(`input'), `\([^;]*\);', `do(`\1')')
18 define(`_chew', `do(substr(`$1', 0, index(`$1', `;')))define(
19 `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'),
20 `;'), -1, `', `$0(defn(`tail'))')')
21 define(`chew', `ifelse(eval($1 < 25), 1, `_$0(`$2')', `$0(eval($1/2),
22 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
23 defn(`tail')substr(`$2', eval($1/2)))')')
24 chew(len(defn(`input')), defn(`input'))