diff options
Diffstat (limited to 'AoC2022/14')
-rw-r--r-- | AoC2022/14/solver.el | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/AoC2022/14/solver.el b/AoC2022/14/solver.el index 0ad36fb..c051f98 100644 --- a/AoC2022/14/solver.el +++ b/AoC2022/14/solver.el @@ -31,13 +31,7 @@ (setq ymin (min ymin (string-to-number (match-string 2))))) (list xmax xmin ymax ymin)))) -(cl-defstruct (grid (:constructor solver-grid-create - (bounds - &aux - (x-len (1+ (- (car bounds) (cadr bounds)))) - (y-len (1+ (elt bounds 2))) - (stride (* 8 (1+ (/ x-len 8)))) - (grid (make-vector (* stride y-len) 0)))) +(cl-defstruct (grid (:constructor solver-grid--create) (:copier nil)) "Represents a snapshot of game of life." bounds @@ -46,9 +40,30 @@ stride grid) + +(defun solver-abyss-grid (bounds) + (let* ((x-len (1+ (- (car bounds) (cadr bounds)))) + (y-len (1+ (elt bounds 2))) + (stride (* 8 (if (= 0 (mod x-len 8)) + (/ x-len 8) + (1+ (/ x-len 8))))) + (grid (make-vector (* stride y-len) 0))) + (solver-grid--create :bounds bounds :x-len x-len :y-len y-len :stride stride :grid grid))) + +(defun solver-finite-grid (bounds) + (let* ((y-len (+ 3 (elt bounds 2))) + (x-len (* 2 y-len)) + (stride (* 8 (if (= 0 (mod x-len 8)) + (/ x-len 8) + (1+ (/ x-len 8))))) + (grid (make-vector (* stride y-len) 0))) + (setcar bounds (+ 500 y-len)) + (setf (elt bounds 1) (- 500 y-len)) + (solver-grid--create :bounds bounds :x-len x-len :y-len y-len :stride stride :grid grid))) + (defun solver-point (x y grid) (let ((x (- x (cadr (grid-bounds grid))))) - (when (and (< -1 (grid-x-len grid)) + (when (and (< -1 x (grid-x-len grid)) (< -1 y (grid-y-len grid))) (+ x (* y (grid-stride grid)))))) @@ -66,7 +81,7 @@ (setq startx fx starty fy))))) -(defun solver-walls (str-or-buffer grid) +(defun solver-walls (str-or-buffer grid &optional finite-grid) (with-temp-buffer (if (stringp str-or-buffer) (insert str-or-buffer) @@ -75,12 +90,15 @@ (while (not (eobp)) (solver--wall-line grid) (forward-line))) + (when finite-grid + (cl-loop with yrow = (* (grid-stride grid) (1- (grid-y-len grid))) + for x from 0 below (grid-x-len grid) + do (aset (grid-grid grid) (+ x yrow) 1))) grid) (defun solver-draw-wall (grid) (let ((array (copy-sequence (grid-grid grid))) - (stride (grid-stride grid)) - (y-len (grid-y-len grid))) + (stride (grid-stride grid))) (create-image array 'xbm t :scale 20 @@ -103,13 +121,6 @@ (2 "o"))))) (grid-grid grid)))) - -(solver-walls "498,4 -> 498,6 -> 496,6 -503,4 -> 502,4 -> 502,9 -> 494,9" - (solver-grid-create '(503 494 9 4))) - -(solver-point 496 3 (solver-grid-create '(503 494 9 4))) - (defun solver-simulate (grid) (let ((x 500) (y 0) (drops 0)) (while @@ -125,16 +136,21 @@ (cl-incf x)) ;; check right (t (aset (grid-grid grid) (solver-point x y grid) 2) (cl-incf drops) - (setq x 500 y 0))))) + (unless (= y 0) + (setq x 500 y 0)))))) drops)) -(defun solver-part1 (instructions) +(defun solver (instructions grid-type) (let* ((bounds (solver-bounds instructions)) - (grid (solver-grid-create bounds))) - (solver-walls instructions grid) + (grid (funcall grid-type bounds))) + (solver-walls instructions grid (eq #'solver-finite-grid grid-type)) (solver-simulate grid))) -(ert-deftest test-part1 () - (should (= 24 (solver-part1 "498,4 -> 498,6 -> 496,6 -503,4 -> 502,4 -> 502,9 -> 494,9"))) - (should (= 665 (solver-part1 (find-file-noselect "input"))))) +(ert-deftest test () + (should (= 24 (solver "498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9" #'solver-abyss-grid))) + (should (= 665 (solver (find-file-noselect "input") #'solver-abyss-grid))) + (should (= 93 (solver "498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9" #'solver-finite-grid))) + (should (= 25434 (solver (find-file-noselect "input") #'solver-finite-grid)))) + |