diff options
-rw-r--r-- | AoC2022/08/solver.lisp | 25 |
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"))) |