aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day14/solve.lisp
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-16 10:17:56 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-16 10:17:56 +0100
commit773bdd5fb9bb389841a110d219d6a54864f2a22e (patch)
tree0b4bb46e4194190ffc5c1a54a45d09d81c4761f1 /AoC2023/day14/solve.lisp
parente51d7a2093cce44c25659b22756f6f05f7044333 (diff)
downloadscratch-773bdd5fb9bb389841a110d219d6a54864f2a22e.tar.gz
scratch-773bdd5fb9bb389841a110d219d6a54864f2a22e.tar.bz2
scratch-773bdd5fb9bb389841a110d219d6a54864f2a22e.zip
[AoC2023] day14 part1
Diffstat (limited to 'AoC2023/day14/solve.lisp')
-rw-r--r--AoC2023/day14/solve.lisp35
1 files changed, 35 insertions, 0 deletions
diff --git a/AoC2023/day14/solve.lisp b/AoC2023/day14/solve.lisp
new file mode 100644
index 0000000..5c463a2
--- /dev/null
+++ b/AoC2023/day14/solve.lisp
@@ -0,0 +1,35 @@
+;; 9:39 start
+;; 10:17 part 1
+;;
+(ql:quickload '(fiveam))
+
+(defun roll-rocks (field)
+ (loop for row from 1 below (array-dimension field 0)
+ sum
+ (loop for col below (array-dimension field 1)
+ when (and (eq #\O (aref field row col))
+ (eq #\. (aref field (1- row) col)))
+ do (setf (aref field (1- row) col) #\O
+ (aref field row col) #\.)
+ and count t)))
+
+(defun array-slice (arr row)
+ (make-array (array-dimension arr 1)
+ :displaced-to arr
+ :displaced-index-offset (* row (array-dimension arr 1))))
+
+(defun solve (input)
+ (let* ((rows (length input))
+ (field
+ (make-array (list rows (length (car input))) :initial-contents input)))
+ (loop
+ for result = (roll-rocks field)
+ until (zerop result))
+ (loop for row below rows
+ sum (* (- rows row) (count #\O (array-slice field row))))))
+
+(fiveam:test solutions
+ (fiveam:is (= 136 (solve (uiop:read-file-lines "eg-in"))))
+ (fiveam:is (= 108935 (solve (uiop:read-file-lines "input")))))
+
+(fiveam:run!)