From 40a24a4f53c8c4940e299f3adc3a18e1c06f0866 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 13 Dec 2022 03:40:55 +0100 Subject: refactor because --- AoC2022/12/solver.lisp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'AoC2022/12') diff --git a/AoC2022/12/solver.lisp b/AoC2022/12/solver.lisp index e0320c3..f4fd602 100644 --- a/AoC2022/12/solver.lisp +++ b/AoC2022/12/solver.lisp @@ -28,22 +28,20 @@ (make-land :elevation (map 'vector #'elevation (apply #'concatenate 'string data)) :neighbors (possible-directions (length (car data)) (length data))))) -(defun appropriate (place elevation land paths) +(defun appropriate (place current land paths) (and (not (gethash place paths)) ;; not visited - (>= elevation (aref (land-elevation land) place)))) + (>= 1 (- (aref (land-elevation land) place) + (aref (land-elevation land) current))) + (setf (gethash place paths) (cons place (gethash current paths))))) (defun next-steps (place land paths) - (let ((elevation (aref (land-elevation land) place))) - (unless (= 27 elevation) - (loop :for option :in (funcall (land-neighbors land) place) - :when (appropriate option (1+ elevation) land paths) - :do (setf (gethash option paths) (cons option (gethash place paths))) - :and collect option)))) + (remove-if-not (lambda (option) (appropriate option place land paths)) + (funcall (land-neighbors land) place))) (defun shortest-path (starts land paths) - (let ((next (mapcan (lambda (place) (next-steps place land paths)) starts))) - (when next - (shortest-path next land paths)))) + (unless (null starts) + (shortest-path (append (cdr starts) (next-steps (car starts) land paths)) + land paths))) (defun solver (input-file start-p) (let* ((land (land input-file)) @@ -62,5 +60,4 @@ (fiveam:is (= 29 (solver "eg-in" (lambda (x) (<= x 1))))) (fiveam:is (= 361 (solver "input" (lambda (x) (= x 0))))) (fiveam:is (= 354 (solver "input" (lambda (x) (<= x 1)))))) - (fiveam:run-all-tests) -- cgit v1.2.3