aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 20:13:46 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 20:13:46 +0100
commit38be9d169ebc49894ac3ff98e042ecb92dc4e232 (patch)
tree2913cb7489fd9284495e5ad22edc02c0457528d5
parentf706d697cb66ef73775af9917586c7d117183cfa (diff)
downloadscratch-38be9d169ebc49894ac3ff98e042ecb92dc4e232.tar.gz
scratch-38be9d169ebc49894ac3ff98e042ecb92dc4e232.tar.bz2
scratch-38be9d169ebc49894ac3ff98e042ecb92dc4e232.zip
Include arrow dependency
-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