diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-16 19:30:13 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-16 19:30:13 +0100 |
commit | 869cca94ef24afeff03b6582c8ac3a522cc866e2 (patch) | |
tree | bdc6d501e519909058747fa4013152757ab3925c /AoC2022/14 | |
parent | 6a6bd6563c6a4fc5c9d018da773bef29d26f4b76 (diff) | |
download | scratch-869cca94ef24afeff03b6582c8ac3a522cc866e2.tar.gz scratch-869cca94ef24afeff03b6582c8ac3a522cc866e2.tar.bz2 scratch-869cca94ef24afeff03b6582c8ac3a522cc866e2.zip |
[AoC2022] Day 14 solve common lisp
Diffstat (limited to 'AoC2022/14')
-rw-r--r-- | AoC2022/14/solver.lisp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/AoC2022/14/solver.lisp b/AoC2022/14/solver.lisp index bb32e0f..188824b 100644 --- a/AoC2022/14/solver.lisp +++ b/AoC2022/14/solver.lisp @@ -28,11 +28,11 @@ (x-len (1+ (- (cadr bounds) (caddr bounds))))) (grid bounds x-len y-len))) -(defun solver-finite-grid (bounds) +(defun make-finite-grid (bounds) (let* ((y-len (+ 3 (car bounds))) (x-len (* 2 y-len))) - (setf (car bounds) (+ 500 y-len)) - (setf (elt bounds 1) (- 500 y-len)) + (setf (elt bounds 1) (+ 500 y-len)) + (setf (elt bounds 2) (- 500 y-len)) (grid bounds x-len y-len))) (defun solver-point (x y grid) @@ -89,14 +89,24 @@ (str:split " -> " row))) list-str)) -(defun solver (list-str) +(defun solver (list-str grid-constructor) (let* ((walls (parse-location list-str)) - (grid (make-abyss-grid (bounds walls)))) + (grid (funcall grid-constructor (bounds walls)))) (solver--wall-line grid walls) - (list (simulate grid) + (when (eq grid-constructor #'make-finite-grid) + (loop with yrow = (* (grid-x-len grid) (1- (grid-y-len grid))) + for x from 0 below (grid-x-len grid) + do (setf (aref (grid-grid grid) (+ x yrow)) 1))) + (values (simulate grid) (draw-grid grid)))) -(solver (uiop:split-string "498,4 -> 498,6 -> 496,6 -503,4 -> 502,4 -> 502,9 -> 494,9" :separator '(#\Newline))) +(fiveam:test test + (let ((eg-data (uiop:split-string "498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9" :separator '(#\Newline)))) + (fiveam:is (= 24 (solver eg-data #'make-abyss-grid))) + (fiveam:is (= 93 (solver eg-data #'make-finite-grid)))) -(solver (uiop:read-file-lines #P"./input")) + (let ((in-data (uiop:read-file-lines "./input"))) + (fiveam:is (= 665 (solver in-data #'make-abyss-grid))) + (fiveam:is (= 25434 (solver in-data #'make-finite-grid))))) +(fiveam:run-all-tests) |