aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-16 11:43:11 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-16 11:43:11 +0100
commita22fc23acdab7504e6db03c7e44dc1dcf22c733d (patch)
treec502b996d4018ea3b2e498960a324d81ba75e01a
parent7a90fab79425ca1b8691beefea4b8337047866ad (diff)
downloadscratch-a22fc23acdab7504e6db03c7e44dc1dcf22c733d.tar.gz
scratch-a22fc23acdab7504e6db03c7e44dc1dcf22c733d.tar.bz2
scratch-a22fc23acdab7504e6db03c7e44dc1dcf22c733d.zip
refactor single slide
-rw-r--r--AoC2023/day14/solve.lisp45
1 files 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!)