aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/12/solver.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/12/solver.lisp')
-rw-r--r--AoC2022/12/solver.lisp21
1 files changed, 9 insertions, 12 deletions
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)