Day 8 - cleanup

This commit is contained in:
Jens Kadenbach
2022-12-08 13:58:34 +01:00
parent b9df0aa858
commit ce3466f746
2 changed files with 12 additions and 16 deletions

View File

@@ -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,7 +56,7 @@ 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
@@ -66,17 +66,14 @@ viewingDistance treeHeight 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)

View File

@@ -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