aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/08
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 18:25:12 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 18:26:41 +0100
commit53fe869e55b2390cba8caef45a2a9559db365386 (patch)
tree4176b3845d4f9372d83c96179aec7fc203ea05aa /AoC2022/08
parent31d848750cbaa568d227595a93e24568ab9dca6a (diff)
downloadscratch-53fe869e55b2390cba8caef45a2a9559db365386.tar.gz
scratch-53fe869e55b2390cba8caef45a2a9559db365386.tar.bz2
scratch-53fe869e55b2390cba8caef45a2a9559db365386.zip
day 8 CL
Diffstat (limited to 'AoC2022/08')
-rw-r--r--AoC2022/08/eg-in5
-rw-r--r--AoC2022/08/solver.lisp50
2 files changed, 55 insertions, 0 deletions
diff --git a/AoC2022/08/eg-in b/AoC2022/08/eg-in
new file mode 100644
index 0000000..16d6fbd
--- /dev/null
+++ b/AoC2022/08/eg-in
@@ -0,0 +1,5 @@
+30373
+25512
+65332
+33549
+35390
diff --git a/AoC2022/08/solver.lisp b/AoC2022/08/solver.lisp
new file mode 100644
index 0000000..e3b63e5
--- /dev/null
+++ b/AoC2022/08/solver.lisp
@@ -0,0 +1,50 @@
+(ql:quickload '(fiveam uiop))
+
+(defun coord (width height)
+ (lambda (x y)
+ (when (and (< -1 x width)
+ (< -1 y height))
+ (+ x (* y width)))))
+
+(let* ((data (uiop:read-file-lines "input"))
+ (width (length (car data)))
+ (height (length data))
+ (forest-arr
+ (apply #'vector
+ (mapcan (lambda (row)
+ (map 'list (lambda (x) (- (char-code x) 48)) row)) data)))
+ (visibility-mask (make-array (* width height) :initial-element nil))
+ (location (coord width height)))
+ (dotimes (y height)
+ ;; left watch
+ (loop
+ for x from 0 below width
+ for maxh = -1 then (max maxh tree-height)
+ for tree-height = (aref forest-arr (funcall location x y))
+ when (> tree-height maxh)
+ do (setf (aref visibility-mask (funcall location x y)) t))
+ ;; right watch
+ (loop
+ for x downfrom (1- width) to 0
+ for maxh = -1 then (max maxh tree-height)
+ for tree-height = (aref forest-arr (funcall location x y))
+ when (> tree-height maxh)
+ do (setf (aref visibility-mask (funcall location x y)) t)))
+
+ (dotimes (x width)
+ ;; top watch
+ (loop
+ for y from 0 below height
+ for maxh = -1 then (max maxh tree-height)
+ for tree-height = (aref forest-arr (funcall location x y))
+ when (> tree-height maxh)
+ do (setf (aref visibility-mask (funcall location x y)) t))
+ ;; bottom watch
+ (loop
+ for y downfrom (1- height) to 0
+ for maxh = -1 then (max maxh tree-height)
+ for tree-height = (aref forest-arr (funcall location x y))
+ when (> tree-height maxh)
+ do (setf (aref visibility-mask (funcall location x y)) t)))
+
+ (loop for v across visibility-mask counting v))