(ql:quickload '(fiveam uiop)) (defun coord-x-minor (width height) (lambda (x y) (when (and (< -1 x width) (< -1 y height)) (+ x (* y width))))) (defun coord-y-minor (width height) (let ((xminor (coord-x-minor width height))) (lambda (y x) (funcall xminor x y)))) (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-x (coord-x-minor width height)) (location-y (coord-y-minor width height)) ) (flet ((toggle-visibility (major minor location reverse) (dotimes (m major) (let ((range (loop for i below minor collect i))) (loop for n in (if reverse (nreverse range) range) for maxh = -1 then (max maxh tree-height) for tree-height = (aref forest-arr (funcall location n m)) when (> tree-height maxh) do (setf (aref visibility-mask (funcall location n m)) t)))))) (toggle-visibility height width location-x nil) (toggle-visibility height width location-x t) (toggle-visibility width height location-y nil) (toggle-visibility width height location-y t) ) (loop for v across visibility-mask counting v) ;; visibility-mask )