aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 19:59:00 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 20:01:41 +0100
commitf706d697cb66ef73775af9917586c7d117183cfa (patch)
treed8da95c19020488ff267d829e6f0ed3e1d4e3da2 /AoC2022
parent1a6b1ab083956a2ecc99b7fac3e51d8a1f0f0db5 (diff)
downloadscratch-f706d697cb66ef73775af9917586c7d117183cfa.tar.gz
scratch-f706d697cb66ef73775af9917586c7d117183cfa.tar.bz2
scratch-f706d697cb66ef73775af9917586c7d117183cfa.zip
cleanup
Diffstat (limited to 'AoC2022')
-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