72 lines
2.6 KiB
Common Lisp
72 lines
2.6 KiB
Common Lisp
;; https://adventofcode.com/2022/day/18
|
|
|
|
(ql:quickload 'cl-ppcre)
|
|
|
|
(defun coords-from-input (file-name)
|
|
(mapcar
|
|
(lambda (line)
|
|
(mapcar #'parse-integer (cl-ppcre:split "," line)))
|
|
(uiop:read-file-lines file-name)))
|
|
|
|
(defun find-maxes (all-coords)
|
|
(loop
|
|
for (x y z) in all-coords
|
|
maximize (+ x 3) into xs
|
|
maximize (+ y 3) into ys
|
|
maximize (+ z 3) into zs
|
|
finally (return (list xs ys zs))) )
|
|
|
|
(defun fill-connectivity-array (all-coords connectivity-matrix)
|
|
(loop
|
|
for (x y z) in all-coords
|
|
do (setf (aref connectivity-matrix x y z) 1)))
|
|
|
|
;; 1 - rock, 0 - initial empty, 2 - outside air
|
|
(defun neighbors-for (coords connectivity-matrix &key (type 1))
|
|
(labels ((coords-fit (potential-point)
|
|
(loop for i from 0 to 2
|
|
always (and (< (nth i potential-point)
|
|
(array-dimension connectivity-matrix i))
|
|
(>= (nth i potential-point) 0)))))
|
|
(loop
|
|
for deltas in `((1 0 0) (-1 0 0)
|
|
(0 1 0) (0 -1 0)
|
|
(0 0 1) (0 0 -1))
|
|
for neighbor = (mapcar #'+ coords deltas)
|
|
when
|
|
(and (coords-fit neighbor)
|
|
(= type (apply #'aref connectivity-matrix neighbor)))
|
|
collect neighbor)))
|
|
|
|
(defun count-open-sides (connectivity-matrix)
|
|
(destructuring-bind (n m k)
|
|
(array-dimensions connectivity-matrix)
|
|
(loop for x from 0 below n sum
|
|
(loop for y from 0 below m sum
|
|
(loop for z from 0 below k
|
|
when (= 1 (aref connectivity-matrix x y z))
|
|
summing (- 6
|
|
(length (neighbors-for (list x y z) connectivity-matrix))))))))
|
|
|
|
(defun point-at-is (coords connectivity-matrix elem)
|
|
(= elem (apply #'aref connectivity-matrix coords)))
|
|
|
|
;; call with initial coord '(0 0 0)
|
|
(defun fill-outside-with-2 (coord connectivity-matrix)
|
|
(when (point-at-is coord connectivity-matrix 0)
|
|
(setf (apply #'aref connectivity-matrix coord) 2)
|
|
(mapcar (lambda (neighbor) (fill-outside-with-2 neighbor connectivity-matrix))
|
|
(neighbors-for coord connectivity-matrix :type 0))))
|
|
|
|
(defun count-open-sides-to-outside (connectivity-matrix)
|
|
(destructuring-bind (n m k)
|
|
(array-dimensions connectivity-matrix)
|
|
(loop for x from 0 below n sum
|
|
(loop for y from 0 below m sum
|
|
(loop for z from 0 below k
|
|
when (= 1 (aref connectivity-matrix x y z))
|
|
summing (length (neighbors-for
|
|
(list x y z)
|
|
connectivity-matrix
|
|
:type 2)))))))
|