aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day14/solve.lisp
blob: 5c463a2efc0bf9a847bb42443fb619a55b9040c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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!)