From abf31c99578dcb117eacf341a56bd9074e4e7a17 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 20 Dec 2023 06:49:26 -0600 Subject: [PATCH] day 19 part 1 ~20ms GNU, ~35ms POSIX. Why write a parser, when I can instead just munge the text into valid m4 macros and let m4 parse it ;) --- 2023/day19.m4 | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2023/day19.m4 diff --git a/2023/day19.m4 b/2023/day19.m4 new file mode 100644 index 0000000..d527380 --- /dev/null +++ b/2023/day19.m4 @@ -0,0 +1,40 @@ +divert(-1)dnl -*- m4 -*- +# Usage: m4 [-Dfile=day19.input] day19.m4 + +include(`common.m4')ifelse(common(19), `ok', `', +`errprint(`Missing common initialization +')m4exit(1)') + +# The input contains the substring nl, but no upper case other than A or R, +# and no lower case vowels. Rename the macros that would interfere. +define(`Nl', defn(`nl'))undefine(`nl') +define(`Dnl', defn(`dnl'))undefine(`dnl') +define(`errprintn', `errprint(`$1'Nl())') + +define(`input', translit((include(defn(`file'))), Nl`,()', `;.')) +define(`part1', 0) +define(`R_') +define(`A_', `define(`part1', eval(part1+x+m+a+s))') +define(`use', `ifelse(`$2', `', `$1', `ifelse(eval($1), 1, `$2', + `$0(shift(shift($@)))')')_()') +define(`run', `define(`x', $1)define(`m', $2)define(`a', $3)define(`s', + $4)in_()popdef(`x')popdef(`m')popdef(`a')popdef(`s')') +define(`do', `run(translit(`$1', `.xmas={}', `,'))') +define(`build', `define(`$1_', `use'(shift($@)))') +pushdef(`do', `ifelse(`$1', `', `popdef(`$0')', `build(translit(`$1', `{:.}', + `,,,'))')') + +ifdef(`__gnu__', ` + patsubst(defn(`input'), `\([^;]*\);', `do(`\1')') +', ` + define(`_chew', `do(substr(`$1', 0, index(`$1', `;')))define( + `tail', substr(`$1', incr(index(`$1', `;'))))ifelse(index(defn(`tail'), + `;'), -1, `', `$0(defn(`tail'))')') + define(`chew', `ifelse(eval($1 < 85), 1, `_$0(`$2')', `$0(eval($1/2), + substr(`$2', 0, eval($1/2)))$0(eval(len(defn(`tail')) + $1 - $1/2), + defn(`tail')substr(`$2', eval($1/2)))')') + chew(len(defn(`input')), defn(`input')) +') + +divert`'part1 +part2 -- 2.11.4.GIT