aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/08
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/08')
-rw-r--r--AoC2022/08/solver.lisp21
1 files changed, 15 insertions, 6 deletions
diff --git a/AoC2022/08/solver.lisp b/AoC2022/08/solver.lisp
index 32d9977..166e56b 100644
--- a/AoC2022/08/solver.lisp
+++ b/AoC2022/08/solver.lisp
@@ -36,6 +36,14 @@
(toggle-visibility width height #'location-y t))
(loop for v across visibility-mask counting v))))
+(defun line-of-sight (direction x y width height)
+ (let ((location (coord-x-minor width height)))
+ (case direction
+ (right (loop for l from (1+ x) below width collect (funcall location l y)))
+ (bottom (loop for l from (1+ y) below height collect (funcall location x l)))
+ (left (loop for l downfrom (1- x) to 0 collect (funcall location l y)))
+ (top (loop for l downfrom (1- y) to 0 collect (funcall location x l))))))
+
(defun solver-p2 (filename)
(multiple-value-bind (width height forest-arr) (forest (uiop:read-file-lines filename))
(let ((score (make-array (* width height)))
@@ -48,12 +56,13 @@
(dotimes (y height)
(dotimes (x width)
(let ((base (aref forest-arr (funcall location-x x y))))
- (setf (aref score (funcall location-x x y))
- (*
- (score-direction base (loop for l from (1+ x) below width collect (funcall location-x l y))) ;; to right
- (score-direction base (loop for l from (1+ y) below height collect (funcall location-x x l))) ;; to bottom
- (score-direction base (loop for l downfrom (1- x) to 0 collect (funcall location-x l y))) ;; to left
- (score-direction base (loop for l downfrom (1- y) to 0 collect (funcall location-x x l)))))))) ;; to top
+ (flet ((sight (direction) (line-of-sight direction x y width height)))
+ (setf (aref score (funcall location-x x y))
+ (*
+ (score-direction base (sight 'right))
+ (score-direction base (sight 'bottom))
+ (score-direction base (sight 'left))
+ (score-direction base (sight 'top))))))))
(loop for v across score maximize v)))))
(fiveam:test solutions