aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/21/solver.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/21/solver.lisp')
-rw-r--r--AoC2022/21/solver.lisp26
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)))