aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/08/solver.lisp30
1 files changed, 14 insertions, 16 deletions
diff --git a/AoC2022/08/solver.lisp b/AoC2022/08/solver.lisp
index 166e56b..9f5c754 100644
--- a/AoC2022/08/solver.lisp
+++ b/AoC2022/08/solver.lisp
@@ -1,4 +1,4 @@
-(ql:quickload '(fiveam uiop))
+(ql:quickload '(fiveam uiop arrows))
(defun coord-x-minor (width height)
(lambda (x y)
@@ -14,6 +14,14 @@
(lambda (x) (- (char-code x) 48))
(apply #'concatenate 'string data)))))
+(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-p1 (filename)
(multiple-value-bind (width height forest-arr) (forest (uiop:read-file-lines filename))
(let ((visibility-mask (make-array (* width height) :initial-element nil))
@@ -36,14 +44,6 @@
(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)))
@@ -56,13 +56,11 @@
(dotimes (y height)
(dotimes (x width)
(let ((base (aref forest-arr (funcall location-x x y))))
- (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))))))))
+ (arrows:->> '(right left top bottom)
+ (mapcar (lambda (direction)
+ (score-direction base (line-of-sight direction x y width height))))
+ (apply #'* )
+ (setf (aref score (funcall location-x x y)))))))
(loop for v across score maximize v)))))
(fiveam:test solutions