aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/08/solver.lisp
blob: 9031e19ca44330800b1f43efab263141927fd4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(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
  )