blob: 9f9d6e8e41e938d442d0f1d85c51afd26830e98c (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
;; 9:39 start
;; 10:17 part 1
;;
(ql:quickload '(fiveam))
(defun roll (direction row col)
(ecase direction
(north (list (1- row) col))
(south (list (1+ row) col))
(west (list row (1- col)))
(east (list row (1+ col)))))
(defun in-bounds (field row col)
(destructuring-bind (maxrow maxcol) (array-dimensions field)
(and (< -1 row maxrow)
(< -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)))
(defun array-slice (arr row)
(make-array (array-dimension arr 1)
:displaced-to arr
:displaced-index-offset (* row (array-dimension arr 1))))
(defun north-load (field)
(loop
with rows = (array-dimension field 0)
for row below rows
sum (* (- rows row) (count #\O (array-slice field row)))))
(defun solve1 (input)
(let* ((rows (length input))
(field
(make-array (list rows (length (car input))) :initial-contents input)))
(roll-rocks-full field 'north)
(north-load field)))
(defun draw-field (field)
(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)))))
(defun solve2 (input iters)
(let* ((rows (length input))
(field
(make-array (list rows (length (car input))) :initial-contents input)))
(loop
do (dolist (dir '(north west south east))
(roll-rocks-full field dir))
repeat iters
collect
(north-load field))))
;; (solve2 (uiop:read-file-lines "eg-in") 1000000000)
;; (solve2 (uiop:read-file-lines "eg-in") 100)
;; (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:run!)
|