From ae7836d3a4172863f1aa7e675c1f1e301bdc2477 Mon Sep 17 00:00:00 2001 From: Matteo Landi Date: Sat, 27 Jan 2024 15:45:28 +0100 Subject: [PATCH] Move MEMOIZING to utils --- package.lisp | 1 + src/2023/day12.lisp | 12 ------------ src/utils.lisp | 10 ++++++++++ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/package.lisp b/package.lisp index a32d8a6..5b69ba9 100644 --- a/package.lisp +++ b/package.lisp @@ -20,6 +20,7 @@ :_2 :partial-2 :defun/memo + :memoizing :with-complex-parts :complex-rotate-cw diff --git a/src/2023/day12.lisp b/src/2023/day12.lisp index fe19409..7dd9571 100644 --- a/src/2023/day12.lisp +++ b/src/2023/day12.lisp @@ -6,18 +6,6 @@ (list springs (extract-positive-integers groups)))) -(defmacro memoizing ((ht &rest key-parts) &body body) - (with-gensyms (memo key) - `(let ((,memo ,ht) - (,key (list ,@key-parts))) - (multiple-value-bind (res res?) (gethash ,key ,memo) - (if res? - res - (setf (gethash ,key ,memo) - (block memo - ,@body))))))) - - (defun count-valid-arrangements (s) (destructuring-bind (springs groups) (parse-condition-record s) (bnd1 (memo (make-hash-table :test 'equal)) diff --git a/src/utils.lisp b/src/utils.lisp index c2d1b8f..d03287a 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -356,6 +356,16 @@ `(defun ,clear-memo-name () (clrhash ,memo)))) +(defmacro memoizing ((ht &rest key-parts) &body body) + (with-gensyms (memo key) + `(let ((,memo ,ht) + (,key (list ,@key-parts))) + (multiple-value-bind (res res?) (gethash ,key ,memo) + (if res? + res + (setf (gethash ,key ,memo) + (block memo + ,@body))))))) ;;;; Math ---------------------------------------------------------------------