Цей коміт міститься в:
Jens Kadenbach
2022-12-08 13:58:34 +01:00
джерело b9df0aa858
коміт ce3466f746
2 змінених файлів з 12 додано та 16 видалено

Переглянути файл

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

Переглянути файл

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