From 53fe869e55b2390cba8caef45a2a9559db365386 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 13 Dec 2022 18:25:12 +0100 Subject: day 8 CL --- AoC2022/08/eg-in | 5 +++++ AoC2022/08/solver.lisp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 AoC2022/08/eg-in create mode 100644 AoC2022/08/solver.lisp (limited to 'AoC2022/08') 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)) -- cgit v1.2.3