aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 00:02:14 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 00:02:14 +0100
commitaa8ad40501c84d5511911339a4daf0a7ea85bbe9 (patch)
tree5aa34463684bc7c81d8863686f64fef4c97cf70a /AoC2022
parentc75d647ee47136853e3e180159d254569d3bba25 (diff)
downloadscratch-aa8ad40501c84d5511911339a4daf0a7ea85bbe9.tar.gz
scratch-aa8ad40501c84d5511911339a4daf0a7ea85bbe9.tar.bz2
scratch-aa8ad40501c84d5511911339a4daf0a7ea85bbe9.zip
[AoC2022] Elisp Day 12 full solution
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/12/eg-in5
-rw-r--r--AoC2022/12/solver.el61
2 files changed, 39 insertions, 27 deletions
diff --git a/AoC2022/12/eg-in b/AoC2022/12/eg-in
new file mode 100644
index 0000000..86e9cac
--- /dev/null
+++ b/AoC2022/12/eg-in
@@ -0,0 +1,5 @@
+Sabqponm
+abcryxxl
+accszExk
+acctuvwj
+abdefghi
diff --git a/AoC2022/12/solver.el b/AoC2022/12/solver.el
index 4a8e149..bb5749a 100644
--- a/AoC2022/12/solver.el
+++ b/AoC2022/12/solver.el
@@ -35,18 +35,17 @@
(when (< 0 x width) (1- pos)) ;; left move
(when (< 0 y height) (- pos width)))))) ;; up move
-(should (equal (solver-directions 6 (land--create :width 5 :height 5)) '(7 11 5 1)))
-(should (equal (solver-directions 0 (land--create :width 5 :height 5)) '(1 5)))
+(ert-deftest test-directions ()
+ (should (equal (solver-directions 6 (land--create :width 5 :height 5)) '(7 11 5 1)))
+ (should (equal (solver-directions 0 (land--create :width 5 :height 5)) '(1 5))))
(defun solver-land (data)
- (vconcat (mapconcat (lambda (row)
- (cl-map 'string (lambda (chr)
- (cl-case chr
- (?S 0)
- (?E 27)
- (t (- chr 96))))
- row))
- data "")))
+ (cl-map 'string (lambda (chr)
+ (cl-case chr
+ (?S 0)
+ (?E 27)
+ (t (- chr 96))))
+ (apply #'concat data)))
(defun solver-next-steps (pos land paths)
(let ((elevation (aref (land-grid land) pos)))
@@ -63,21 +62,29 @@
(when-let ((next (mapcan (lambda (place) (solver-next-steps place land paths)) queue)))
(solver-search next land paths)))
-(with-temp-buffer
-;; (insert "Sabqponm
-;; abcryxxl
-;; accszExk
-;; acctuvwj
-;; abdefghi")
- (insert-file-contents "input")
- (let* ((data (split-string (buffer-string) "\n" t))
- (height (length data))
- (width (length (car data)))
- (land (land--create :grid (solver-land data) :width width :height height))
- (start (seq-position (land-grid land) 0 #'eq))
+(defun solver-landscape (filename)
+ (with-temp-buffer
+ (insert "")
+ (insert-file-contents filename)
+ (let* ((data (split-string (buffer-string) "\n" t))
+ (height (length data))
+ (width (length (car data))))
+ (land--create :grid (solver-land data) :width width :height height))))
+
+(defun solver (input-file start-p)
+ (let* ((land (solver-landscape input-file))
+ (paths (make-hash-table :test #'eq))
(finish (seq-position (land-grid land) 27 #'eq))
- (paths (make-hash-table :test #'eq)))
- (puthash start (list start) paths)
- (solver-search (list start) land paths)
- (1- (length (gethash finish paths)))
- ))
+ (starts
+ (cl-loop for i from 0
+ for el across (land-grid land)
+ when (funcall start-p el)
+ collect (progn (puthash i (list i) paths) i))))
+ (solver-search starts land paths)
+ (1- (length (gethash finish paths)))))
+
+(ert-deftest test-solver ()
+ (should (= 31 (solver "eg-in" (lambda (el) (= el 0)))))
+ (should (= 29 (solver "eg-in" (lambda (el) (<= el 1)))))
+ (should (= 361 (solver "input" (lambda (el) (= el 0)))))
+ (should (= 354 (solver "input" (lambda (el) (<= el 1))))))