Day 8 - cleanup
This commit is contained in:
25
src/Day8.hs
25
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
|
let row = M.getRow x forest
|
||||||
col = M.getCol y forest
|
col = M.getCol y forest
|
||||||
-- left and top are reversed to order the as seen from (x,y)
|
-- 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
|
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
|
bottom = V.drop x col
|
||||||
v = forest ! (x,y)
|
v = forest ! (x,y)
|
||||||
in f v [left, right, top, bottom]
|
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 :: 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,29 +56,26 @@ 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
|
||||||
| V.length neighbors == 0 = 0
|
| V.length neighbors == 0 = 0
|
||||||
| V.head neighbors >= treeHeight = 1
|
| V.head neighbors >= treeHeight = 1
|
||||||
| V.head neighbors < treeHeight = 1 + viewingDistance treeHeight (V.tail neighbors)
|
| V.head neighbors < treeHeight = 1 + viewingDistance treeHeight (V.tail 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)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user