From 869cca94ef24afeff03b6582c8ac3a522cc866e2 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 16 Dec 2022 19:30:13 +0100 Subject: [AoC2022] Day 14 solve common lisp --- AoC2022/14/solver.lisp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'AoC2022') 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) -- cgit v1.2.3