From 5f0101dd3c9859bacc397dc62e9ec29e20ebebe0 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 8 Dec 2023 13:45:14 +0100 Subject: refactor --- AoC2023/day08/solver.lisp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'AoC2023/day08/solver.lisp') diff --git a/AoC2023/day08/solver.lisp b/AoC2023/day08/solver.lisp index 0681e15..76001f1 100644 --- a/AoC2023/day08/solver.lisp +++ b/AoC2023/day08/solver.lisp @@ -18,21 +18,15 @@ ZZZ = (ZZZ, ZZZ)") (values instructions graph)))) (defun traverse (instructions graph location terminatep) - (labels ((rec (steps) - (if (funcall terminatep location) - (values steps location) - (rec (+ steps (steps-needed))))) - (steps-needed () - (loop - for direction across instructions - do (arrows:-<> - (ecase direction - (#\L #'first) - (#\R #'second)) - (funcall <> (gethash location graph)) - (setf location <>)) - count direction until (funcall terminatep location)))) - (rec 0))) + (loop + for i from 0 + do (arrows:-<> + (ecase (aref instructions (mod i (length instructions))) + (#\L #'first) + (#\R #'second)) + (funcall <> (gethash location graph)) + (setf location <>)) + count i until (funcall terminatep location))) (defun solver1 (lines) (multiple-value-bind (instructions graph) -- cgit v1.2.3