aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/21/solver.lisp
blob: fa28e31970604892f86fa64aa20c61bc05c00f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)))