From 2d21b30679b06949891f99d5dadffbb48ebbc2d3 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 12 Dec 2022 22:26:12 +0100 Subject: same depth --- AoC2022/12/solver.el | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'AoC2022/12/solver.el') 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-)) -- cgit v1.2.3