day 18, part 2, not complete, before going ugly

This commit is contained in:
efim
2022-12-22 20:37:14 +00:00
parent 643bba2464
commit 831f09c9cd
2 changed files with 83 additions and 5 deletions

View File

@@ -11,9 +11,9 @@
(defun find-maxes (all-coords)
(loop
for (x y z) in all-coords
maximize (1+ x) into xs
maximize (1+ y) into ys
maximize (1+ z) into zs
maximize (+ x 2) into xs
maximize (+ y 2) into ys
maximize (+ z 2) into zs
finally (return (list xs ys zs))) )
(defun fill-connectivity-array (all-coords connectivity-matrix)
@@ -21,7 +21,8 @@
for (x y z) in all-coords
do (setf (aref connectivity-matrix x y z) 1)))
(defun neighbors-for (coords connectivity-matrix)
;; 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)
@@ -34,7 +35,7 @@
for neighbor = (mapcar #'+ coords deltas)
when
(and (coords-fit neighbor)
(= 1 (apply #'aref connectivity-matrix neighbor)))
(= type (apply #'aref connectivity-matrix neighbor)))
collect neighbor)))
(defun count-open-sides (connectivity-matrix)
@@ -46,3 +47,25 @@
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)))))))