diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-01-13 18:33:01 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-01-13 18:33:01 +0100 |
commit | a03b7549a73f016a4dd854bc9346a9bca155ffdc (patch) | |
tree | 3d0b63ad01b8e51f3f4350f774b373ddfdee9f97 /AoC2022/21/solver.lisp | |
parent | 25957c30efdefce0c349a9ddf9100a458fa3e6ab (diff) | |
download | scratch-a03b7549a73f016a4dd854bc9346a9bca155ffdc.tar.gz scratch-a03b7549a73f016a4dd854bc9346a9bca155ffdc.tar.bz2 scratch-a03b7549a73f016a4dd854bc9346a9bca155ffdc.zip |
day 21
Diffstat (limited to 'AoC2022/21/solver.lisp')
-rw-r--r-- | AoC2022/21/solver.lisp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/AoC2022/21/solver.lisp b/AoC2022/21/solver.lisp new file mode 100644 index 0000000..fa28e31 --- /dev/null +++ b/AoC2022/21/solver.lisp @@ -0,0 +1,26 @@ +(ql:quickload '(fiveam uiop cl-ppcre trivia)) + +(defun build-monkey-table (filename) + (let ((table (make-hash-table :test #'eq))) + (with-open-file (stream filename) + (loop for line = (read-line stream nil nil) + while line + do + (cond + ((ppcre:register-groups-bind ((#'read-from-string monkey dep1 op dep2)) + ("(\\w+): (\\w+) ([*+/-]) (\\w+)" line) + (setf (gethash monkey table) (list op dep1 dep2)))) + ((ppcre:register-groups-bind ((#'read-from-string monkey number)) + ("(\\w+): (\\d+)" line) + (setf (gethash monkey table) number)))))) + table)) + +(defun resolver (entry table) + (trivia:match entry + ((list op a b) (funcall op + (resolver (gethash a table) table) + (resolver (gethash b table) table))) + (a a))) + +(let ((table (build-monkey-table "input"))) + (= 56490240862410 (resolver (gethash 'root table) table))) |