From f706d697cb66ef73775af9917586c7d117183cfa Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 13 Dec 2022 19:59:00 +0100 Subject: cleanup --- AoC2022/08/solver.lisp | 21 +++++++++++++++------ 1 file 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 -- cgit v1.2.3