From 62e05f0717a2173e729cf10b4f29edc1270d6278 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 8 Dec 2023 12:41:42 +0100 Subject: [AoC2023] day08 lisp part1 --- AoC2023/day08/solver.lisp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 AoC2023/day08/solver.lisp (limited to 'AoC2023/day08/solver.lisp') 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"))))) -- cgit v1.2.3