From aa8ad40501c84d5511911339a4daf0a7ea85bbe9 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 13 Dec 2022 00:02:14 +0100 Subject: [AoC2022] Elisp Day 12 full solution --- AoC2022/12/solver.el | 61 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'AoC2022/12/solver.el') 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)))))) -- cgit v1.2.3