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 :: Forest -> Visibillity
checkVisibility = checkAgainstNeighbours isVisible checkVisibility = checkAgainstNeighbours isVisible
where where
isVisible v = any (all (< v)) isVisible treeHeight = any (all (< treeHeight))
countVisible :: Visibillity -> Int countVisible :: Visibillity -> Int
countVisible vis = getSum $ foldMap (Sum . isTrue) vis countVisible = foldMap (Sum . isTrue) >>> getSum
where where
isTrue True = 1 isTrue True = 1
isTrue False = 0 isTrue False = 0
@@ -56,7 +56,7 @@ countVisible vis = getSum $ foldMap (Sum . isTrue) vis
checkScenicScores :: Forest -> M.Matrix Int checkScenicScores :: Forest -> M.Matrix Int
checkScenicScores = checkAgainstNeighbours allDistances checkScenicScores = checkAgainstNeighbours allDistances
where 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 :: Int -> V.Vector Int -> Int
viewingDistance treeHeight neighbors viewingDistance treeHeight neighbors
@@ -66,17 +66,14 @@ viewingDistance treeHeight neighbors
| otherwise = 0 | otherwise = 0
findMaxScenicScore :: Forest -> Int findMaxScenicScore :: Forest -> Int
findMaxScenicScore forest = maximum $ M.toList scores findMaxScenicScore = checkScenicScores >>> M.toList >>> maximum
where
scores = checkScenicScores forest
day8 :: IO () day8 :: IO ()
day8 = do day8 = do
input <- readFile "ressources/day08-input" input <- readFile "ressources/day08-input"
putStrLn "Day8" putStrLn "Day8"
let forest = parseForest input let forest = parseForest input
let visibleTrees = checkVisibility >>> let visibleTrees = checkVisibility >>> countVisible $ forest
countVisible $ forest
putStrLn ("Number of visible trees is " ++ show visibleTrees) putStrLn ("Number of visible trees is " ++ show visibleTrees)
let highestScenicScore = findMaxScenicScore forest let highestScenicScore = findMaxScenicScore forest
putStrLn ("Highest scenic score is " ++ show highestScenicScore) 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,x,o,x,
x,x,o,x,x, x,x,o,x,x,
x,o,x,o,x, x,o,x,o,x,
x,x,x,x,x x,x,x,x,x]
]
where where
x = True x = True
o = False o = False