aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 21:25:24 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 21:25:24 +0100
commit7b4efed3226d20cb0a971060d43b4914c81fe9fa (patch)
tree1462b0d7c5b20576561496ed76d9e68176fc179a
parent8cd8a952967e7465753c173dbfbbf906b8b58370 (diff)
downloadscratch-7b4efed3226d20cb0a971060d43b4914c81fe9fa.tar.gz
scratch-7b4efed3226d20cb0a971060d43b4914c81fe9fa.tar.bz2
scratch-7b4efed3226d20cb0a971060d43b4914c81fe9fa.zip
cleanup and drop arrows
-rw-r--r--AoC2022/08/solver.lisp25
1 files changed, 14 insertions, 11 deletions
diff --git a/AoC2022/08/solver.lisp b/AoC2022/08/solver.lisp
index 05353f0..3797589 100644
--- a/AoC2022/08/solver.lisp
+++ b/AoC2022/08/solver.lisp
@@ -1,4 +1,4 @@
-(ql:quickload '(fiveam uiop arrows))
+(ql:quickload '(fiveam uiop))
(defun line-of-sight (direction p width height)
(case direction
@@ -16,6 +16,8 @@
(apply #'concatenate 'string data)))))
(defun solver-p1 (filename)
+ "Count how many trees are visible from the outside.
+A tree is visible if all other trees between it and the edge of the grid are shorter."
(multiple-value-bind (width height forest-arr) (forest (uiop:read-file-lines filename))
(loop for base across forest-arr
and p from 0
@@ -26,17 +28,18 @@
'(right left top bottom)))))
(defun solver-p2 (filename)
+ "A scenic score is the product of the viewing distance in each direction.
+The viewing distance is the lenght until a tree of equal height appears."
(multiple-value-bind (width height forest-arr) (forest (uiop:read-file-lines filename))
- (flet ((score-direction (base dir)
- (loop for l in dir
- for tree-height = (aref forest-arr l)
- count l until (>= tree-height base))))
- (loop for base across forest-arr
- and p from 0
- maximize (arrows:->> '(right left top bottom)
- (mapcar (lambda (direction)
- (score-direction base (line-of-sight direction p width height))))
- (apply #'* ))))))
+ (loop for base across forest-arr
+ and p from 0
+ maximize (apply #'*
+ (mapcar
+ (lambda (direction)
+ (loop for l in (line-of-sight direction p width height)
+ for tree-height = (aref forest-arr l)
+ count l until (>= tree-height base)))
+ '(right left top bottom))))))
(fiveam:test solutions
(fiveam:is (= 21 (solver-p1 "eg-in")))