(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)))