aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-12 22:26:12 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-12 22:26:12 +0100
commit2d21b30679b06949891f99d5dadffbb48ebbc2d3 (patch)
treee1442f5b9e8d7974f8fafcfdec36501af7abc27d
parent6a52d96ee87b07ed890e7fa77ecc35b8fae0131c (diff)
downloadscratch-2d21b30679b06949891f99d5dadffbb48ebbc2d3.tar.gz
scratch-2d21b30679b06949891f99d5dadffbb48ebbc2d3.tar.bz2
scratch-2d21b30679b06949891f99d5dadffbb48ebbc2d3.zip
same depth
-rw-r--r--AoC2022/12/solver.el47
1 files changed, 32 insertions, 15 deletions
diff --git a/AoC2022/12/solver.el b/AoC2022/12/solver.el
index 44fe525..1c68d04 100644
--- a/AoC2022/12/solver.el
+++ b/AoC2022/12/solver.el
@@ -24,8 +24,8 @@
(delq nil
(list
(when (< -1 x (1- width)) (1+ pos)) ;; right move
- (when (< 0 x width) (1- pos)) ;; left move
(when (< -1 y (1- height)) (+ pos width)) ;; down move
+ (when (< 0 x width) (1- pos)) ;; left move
(when (< 0 y height) (- pos width)))))) ;; up move
(should (equal (solver-directions 6 5 5) '(7 5 11 1)))
@@ -41,17 +41,27 @@
row))
data "")))
-(defun solver-search (path land)
+(defun solver-next-steps (path land)
(let* ((pos (car path))
- (elevation (aref land pos))
- (next-cells (solver-directions pos width height)))
+ (elevation (aref land pos)))
(if (= 27 elevation) ;; reached destination
- (list (reverse path))
+ (list (vconcat (reverse path)))
(mapcan (lambda (option)
(when (and (not (memq option path))
(>= (1+ elevation) (aref land option))) ;; allowed move
- (solver-search (cons option path) land)))
- next-cells))))
+ (list (cons option path))))
+ (solver-directions pos width height)))))
+
+
+(defun solver-search (queue land)
+
+ (let* ((next (mapcan (lambda (path) (solver-next-steps path land)) queue))
+ (finish (seq-filter #'vectorp next)))
+ (cond
+ ((null next) "No solution")
+ ((null finish) (solver-search next land))
+ (finish))
+ ))
(with-temp-buffer
(insert "Sabqponm
@@ -64,12 +74,19 @@ abdefghi")
(width (length (car data)))
(land (solver-land data))
(start (seq-position land 0 #'eq)))
- (thread-first
- (cl-sort
- (solver-search (list start) land)
- #'<
- :key #'length)
- (car)
- (length)
- (1-))
+ ;; (solver-search
+ ;; land)
+ (let ((res (solver-search (list (list start)) land)))
+ (list
+ (length res)
+ (mapcar #'length res))
+ )
))
+
+(thread-first
+ (cl-sort
+ #'<
+ :key #'length)
+ (car)
+ (length)
+ (1-))