aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022')
-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))