aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/12
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 03:40:55 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 04:08:20 +0100
commit40a24a4f53c8c4940e299f3adc3a18e1c06f0866 (patch)
treeafad5f681b1702bf307a155e950e7b43b2345e22 /AoC2022/12
parenteb382643923041b00a032ae71cac8b40dc716698 (diff)
downloadscratch-40a24a4f53c8c4940e299f3adc3a18e1c06f0866.tar.gz
scratch-40a24a4f53c8c4940e299f3adc3a18e1c06f0866.tar.bz2
scratch-40a24a4f53c8c4940e299f3adc3a18e1c06f0866.zip
refactor because
Diffstat (limited to 'AoC2022/12')
-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)