1 divert(-1)dnl -*- m4 -*-
2 # Usage: m4 [-Dfile=day7.input] day7.m4
4 include(`common.m4')ifelse(common(7), `ok', `',
5 `errprint(`Missing common initialization
8 define(`AND', `&')define(`OR', `|')define(`LSHIFT', `<<')define(`RSHIFT', `>>')
9 define(`list', translit(include(defn(`file')), nl, `;'))
10 define(`memo', `define(`$1', `define(`$1', eval($2))$1`'')')
11 define(`line', `_$0(translit(`$1', ` ', `,'))')
12 define(`_line', `ifelse(`$#', 3, `memo(`$3_', `$1_')memo(`$3__', `$1__')',
13 `$#', 4, `memo(`$4_', `~$2_ & `0xffff'')memo(`$4__', `~$2__ & `0xffff'')',
14 `memo(`$5_', `($1_ $2 $3_) & `0xffff'')memo(`$5__',
15 `($1__ $2 $3__) & `0xffff'')')')
16 define(`_')define(`__')
18 patsubst(defn(`list'), `\([^;]*\);', `line(`\1')')
20 define(`chew', `line(substr(`$1', 0, index(`$1', `;')))define(`tail',
21 substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(
22 `tail'), `;'), -1, `', `$0(defn(`tail'))')')
23 define(`split', `ifelse(eval($1 < 40), 1, `chew(`$2')', `$0(eval($1/2),
24 substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2),
25 defn(`tail')substr(`$2', eval($1/2)))')')
26 split(len(defn(`list')), defn(`list'))
28 define(`part1', a_)define(`b__', part1)define(`part2', a__)