From ce3466f7466e21bc3d8e1d2fb2c1992cdb0c80e0 Mon Sep 17 00:00:00 2001 From: Jens Kadenbach Date: Thu, 8 Dec 2022 13:58:34 +0100 Subject: [PATCH] Day 8 - cleanup --- src/Day8.hs | 25 +++++++++++-------------- test/Day8Spec.hs | 3 +-- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Day8.hs b/src/Day8.hs index b116228..a1a27a5 100644 --- a/src/Day8.hs +++ b/src/Day8.hs @@ -35,9 +35,9 @@ checkAgainstNeighbours f forest = M.matrix (M.nrows forest) (M.ncols forest) ch let row = M.getRow x forest col = M.getCol y forest -- left and top are reversed to order the as seen from (x,y) - left = V.reverse $ V.take (y - 1) row + left = V.reverse $ V.take (y - 1) row right = V.drop y row - top = V.reverse $ V.take (x - 1) col + top = V.reverse $ V.take (x - 1) col bottom = V.drop x col v = forest ! (x,y) in f v [left, right, top, bottom] @@ -45,10 +45,10 @@ checkAgainstNeighbours f forest = M.matrix (M.nrows forest) (M.ncols forest) ch checkVisibility :: Forest -> Visibillity checkVisibility = checkAgainstNeighbours isVisible where - isVisible v = any (all (< v)) + isVisible treeHeight = any (all (< treeHeight)) countVisible :: Visibillity -> Int -countVisible vis = getSum $ foldMap (Sum . isTrue) vis +countVisible = foldMap (Sum . isTrue) >>> getSum where isTrue True = 1 isTrue False = 0 @@ -56,29 +56,26 @@ countVisible vis = getSum $ foldMap (Sum . isTrue) vis checkScenicScores :: Forest -> M.Matrix Int checkScenicScores = checkAgainstNeighbours allDistances where - allDistances tree neighbors = getProduct $ foldMap (Product . viewingDistance tree) neighbors + allDistances treeHeight neighbors = foldMap (viewingDistance treeHeight >>> Product) >>> getProduct $ neighbors viewingDistance :: Int -> V.Vector Int -> Int viewingDistance treeHeight neighbors - | V.length neighbors == 0 = 0 - | V.head neighbors >= treeHeight = 1 - | V.head neighbors < treeHeight = 1 + viewingDistance treeHeight (V.tail neighbors) + | V.length neighbors == 0 = 0 + | V.head neighbors >= treeHeight = 1 + | V.head neighbors < treeHeight = 1 + viewingDistance treeHeight (V.tail neighbors) | otherwise = 0 findMaxScenicScore :: Forest -> Int -findMaxScenicScore forest = maximum $ M.toList scores - where - scores = checkScenicScores forest +findMaxScenicScore = checkScenicScores >>> M.toList >>> maximum day8 :: IO () day8 = do input <- readFile "ressources/day08-input" putStrLn "Day8" let forest = parseForest input - let visibleTrees = checkVisibility >>> - countVisible $ forest + let visibleTrees = checkVisibility >>> countVisible $ forest putStrLn ("Number of visible trees is " ++ show visibleTrees) let highestScenicScore = findMaxScenicScore forest putStrLn ("Highest scenic score is " ++ show highestScenicScore) - + diff --git a/test/Day8Spec.hs b/test/Day8Spec.hs index ceaf9cf..da27cd8 100644 --- a/test/Day8Spec.hs +++ b/test/Day8Spec.hs @@ -23,8 +23,7 @@ testVisibility = M.fromList 5 5 x,x,x,o,x, x,x,o,x,x, x,o,x,o,x, - x,x,x,x,x - ] + x,x,x,x,x] where x = True o = False