aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day08/solver.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2023/day08/solver.lisp')
-rw-r--r--AoC2023/day08/solver.lisp41
1 files changed, 41 insertions, 0 deletions
diff --git a/AoC2023/day08/solver.lisp b/AoC2023/day08/solver.lisp
new file mode 100644
index 0000000..b552ef5
--- /dev/null
+++ b/AoC2023/day08/solver.lisp
@@ -0,0 +1,41 @@
+(ql:quickload '(fiveam str))
+ ;12:13
+ ;
+(defparameter eg-input "LLR
+
+AAA = (BBB, BBB)
+BBB = (AAA, ZZZ)
+ZZZ = (ZZZ, ZZZ)")
+
+(defun parse-input (lines)
+ (let ((graph (make-hash-table)))
+ (destructuring-bind (instructions blank . nodes) lines
+ (declare (ignorable blank))
+ (dolist (line nodes)
+ (with-input-from-string (in (str:replace-all "=|," "" line :regex t))
+ (setf
+ (gethash (read in) graph)
+ (read in))))
+ (values instructions graph))))
+
+(defun traverse (instructions graph location steps)
+ (if (eq location 'ZZZ)
+ (values steps location)
+ (let ((new-steps (loop
+ for direction across instructions
+ do
+ (setf location
+ (ecase direction
+ (#\L (first (gethash location graph)))
+ (#\R (second (gethash location graph)))))
+ count direction until (eq location 'ZZZ))))
+ (traverse instructions graph location (+ steps new-steps)))))
+
+(defun solver1 (lines)
+ (multiple-value-bind (instructions graph)
+ (parse-input lines)
+ (traverse instructions graph 'AAA 0)))
+
+(fiveam:test solutions
+ (fiveam:is (= 6 (solver1 (uiop:split-string eg-input :separator '(#\Newline)))))
+ (fiveam:is (= 19199 (solver1 (uiop:read-file-lines "input")))))