From a22fc23acdab7504e6db03c7e44dc1dcf22c733d Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sat, 16 Dec 2023 11:43:11 +0100 Subject: refactor single slide --- AoC2023/day14/solve.lisp | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/AoC2023/day14/solve.lisp b/AoC2023/day14/solve.lisp index 9f9d6e8..e476bd5 100644 --- a/AoC2023/day14/solve.lisp +++ b/AoC2023/day14/solve.lisp @@ -16,22 +16,17 @@ (< -1 col maxcol)))) (defun roll-rocks (field direction) - (loop for row below (array-dimension field 0) - sum - (loop for col below (array-dimension field 1) - for (new-row new-col) = (roll direction row col) - when (and - (in-bounds field new-row new-col) - (eq #\O (aref field row col)) - (eq #\. (aref field new-row new-col))) - do (setf (aref field new-row new-col) #\O - (aref field row col) #\.) - and count t))) - -(defun roll-rocks-full (field direction) - (loop - for result = (roll-rocks field direction) - until (zerop result))) + (loop for i below (array-dimension field 0) do + (loop for j below (array-dimension field 1) do + (loop + for (row col) = (list i j) then (list new-row new-col) + for (new-row new-col) = (roll direction row col) + while (and + (in-bounds field new-row new-col) + (eq #\O (aref field row col)) + (eq #\. (aref field new-row new-col))) + do (setf (aref field new-row new-col) #\O + (aref field row col) #\.))))) (defun array-slice (arr row) (make-array (array-dimension arr 1) @@ -48,17 +43,16 @@ (let* ((rows (length input)) (field (make-array (list rows (length (car input))) :initial-contents input))) - (roll-rocks-full field 'north) + (roll-rocks field 'north) (north-load field))) -(defun draw-field (field) +(defun draw-field (field &optional (out *standard-output*)) (destructuring-bind (maxrow maxcol) (array-dimensions field) - (with-output-to-string (out) - - (loop for i below maxrow do - (loop for j below maxcol do - (write-char (aref field i j) out)) - (terpri out))))) + (loop for i below maxrow do + (loop for j below maxcol do + (write-char (aref field i j) out)) + (terpri out)) + (terpri out))) (defun solve2 (input iters) (let* ((rows (length input)) @@ -76,6 +70,7 @@ ;; (solve2 (uiop:read-file-lines "input") 100) (fiveam:test solutions (fiveam:is (= 136 (solve1 (uiop:read-file-lines "eg-in")))) - (fiveam:is (= 108935 (solve1 (uiop:read-file-lines "input"))))) + (fiveam:is (= 108935 (solve1 (uiop:read-file-lines "input")))) + ) (fiveam:run!) -- cgit v1.2.3