aboutsummaryrefslogtreecommitdiff
path: root/2024/15/puzzle-2.lisp
blob: bd3f8e5432fe433b953911897af7ccca91aed15d (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/sbcl --script

(defparameter *map*       nil)
(defparameter *moves*     nil)
(defparameter *robot-pos* nil)

(defun main (filename)
  (parse-input filename)
  (mapc #'handle-move *moves*)
  (loop with (my mx) = (array-dimensions *map*)
        for y from 0 below my
        sum (loop for x from 0 below mx
                  if (box-at-pos-p (cons x y))
                    sum (+ x (* 100 y)))))

(defun handle-move (v)
  (when (can-move-p v)
    (map-setf *robot-pos* #\.)
    (setq *robot-pos* (vec2+ *robot-pos* v))
    (let ((dest-char (aref-vec2 *map* *robot-pos*)))
      (case dest-char
        (#\[ (handle-box-move *robot-pos* v))
        (#\] (handle-box-move (left-of *robot-pos*) v))))
    (map-setf *robot-pos* #\@)))

(defun handle-box-move (pos v)
  (cond
    ((equal v '(+1 . 0))
     (let ((next-box-pos (right-of pos 2)))
       (when (box-at-pos-p next-box-pos)
         (handle-box-move next-box-pos v)))
     (map-setf (right-of pos 1) #\[)
     (map-setf (right-of pos 2) #\]))
    ((equal v '(-1 . 0))
     (let ((next-box-pos (left-of pos 2)))
       (when (box-at-pos-p next-box-pos)
         (handle-box-move next-box-pos v)))
     (map-setf pos #\])
     (map-setf (left-of pos) #\[))
    (:else                 ; (or (equal v⃗ '(0 . +1)) (equal v⃗ '(0 . -1)))
     (let* ((dy (cdr v))
            (charl (aref-vec2 *map* (vec2+ pos (cons 0 dy))))
            (charr (aref-vec2 *map* (vec2+ pos (cons 1 dy)))))
       (when (char= charl #\[)
         (handle-box-move (vec2+ pos v) v))
       (when (char= charl #\])
         (handle-box-move (vec2+ pos (cons -1 dy)) v))
       (when (char= charr #\[)
         (handle-box-move (vec2+ pos (cons +1 dy)) v))
       (map-setf pos #\.)
       (map-setf (right-of pos) #\.)
       (map-setf (vec2+ pos (cons 0 dy)) #\[)
       (map-setf (vec2+ pos (cons 1 dy)) #\])))))

(defun box-at-pos-p (pos)
  (char= (aref-vec2 *map* pos) #\[))

(defun can-move-p (v)
  (let ((pos (vec2+ *robot-pos* v)))
    (ecase (aref-vec2 *map* pos)
      (#\. t)
      (#\# nil)
      (#\[ (can-move-box-p pos v))
      (#\] (can-move-box-p (left-of pos) v)))))

(defun can-move-box-p (pos v)
  (cond
    ((equal v '(1 . 0))
     (let ((next-box-pos (right-of pos 2)))
       (ecase (aref-vec2 *map* next-box-pos)
         (#\. t)
         (#\# nil)
         (#\[ (can-move-box-p next-box-pos v)))))
    ((equal v '(-1 . 0))
     (ecase (aref-vec2 *map* (left-of pos))
       (#\. t)
       (#\# nil)
       (#\[
        (print *map*)
        (quit))
       (#\] (can-move-box-p (left-of pos 2) v))))
    (:else                 ; (or (equal v⃗ '(0 . +1)) (equal v⃗ '(0 . -1)))
     (let* ((dy (cdr v))
            (charl (aref-vec2 *map* (vec2+ pos (cons 0 dy))))
            (charr (aref-vec2 *map* (vec2+ pos (cons 1 dy)))))
       (cond ((char= #\. charl charr)
              t)
             ((or (char= #\# charl)
                  (char= #\# charr))
              nil)
             ((char= #\[ charl)
              (can-move-box-p (vec2+ pos v) v))
             (:else            ; (or (char= #\] charl) (char= #\[ charr))
              (and (or (char/= #\] charl)
                       (can-move-box-p (vec2+ pos (cons -1 dy)) v))
                   (or (char/= #\[ charr)
                       (can-move-box-p (vec2+ pos (cons +1 dy)) v)))))))))

(defun in-map-bounds-p (pos)
  (destructuring-bind (y x) (array-dimensions *map*)
    (and (< -1 (car pos) x)
         (< -1 (cdr pos) y))))

(defun aref-vec2 (array v)
  (aref array (cdr v) (car v)))

(defun map-setf (pos val)
  (setf (aref *map* (cdr pos) (car pos)) val))

;;; 2D Vectors

(defun vec2+ (v u)
  (cons (+ (car v) (car u))
        (+ (cdr v) (cdr u))))

(defun left-of (v &optional (n 1))
  (vec2+ v (cons (- n) 0)))

(defun right-of (v &optional (n 1))
  (vec2+ v (cons n 0)))

(defun up-from (v &optional (n 1))
  (vec2+ v (cons 0 (- n))))

(defun down-from (v &optional (n 1))
  (vec2+ v (cons 0 n)))

;;; Parsing

(defun parse-input (filename)
  (with-open-file (stream filename)
    (let ((contents (make-string (file-length stream))))
      (read-sequence contents stream)
      (destructuring-bind (map moves)
          (split-string contents (format nil "~%~%"))
        (multiple-value-bind (map start-pos) (parse-map map)
          (setq *map* map
                *moves* (parse-moves moves)
                *robot-pos* start-pos))))))

(defun parse-map (string)
  (let* ((first-newline (position #\Newline string))
         (line-length (1+ first-newline))
         (line-count (/ (1+ (length string)) line-length))
         (map (make-array (list line-count (* 2 first-newline))
                          :element-type 'character))
         start-pos)
    (dotimes (y line-count)
      (loop with start = (* y line-length)
            for char across (subseq string start (+ start first-newline))
            for x = 0 then (+ x 2)
            do (ecase char
                 (#\# (setf (aref map y (+ 0 x)) #\#
                            (aref map y (+ 1 x)) #\#))
                 (#\O (setf (aref map y (+ 0 x)) #\[
                            (aref map y (+ 1 x)) #\]))
                 (#\. (setf (aref map y (+ 0 x)) #\.
                            (aref map y (+ 1 x)) #\.))
                 (#\@ (setf (aref map y (+ 0 x)) #\@
                            (aref map y (+ 1 x)) #\.
                            start-pos (cons x y))))))
    (values map start-pos)))

(defun parse-moves (string)
  (loop for char across string
        unless (char= char #\Newline)
          collect (ecase char
                    (#\^ '(0 . -1))
                    (#\v '(0 . +1))
                    (#\< '(-1 . 0))
                    (#\> '(+1 . 0)))))

(defun split-string (string delimiter)
  (loop with delimiter-length = (length delimiter)
        for end = (search delimiter string)
        unless (eq end 0)
          collect (subseq string 0 end) into parts
        unless end
          return parts
        do (setq string (subseq string (+ end delimiter-length)))))

(format t "~d~%" (main "input"))