--- Writeup at https://work.njae.me.uk/2022/12/07/advent-of-code-2022-day-7/
+-- Writeup at https://work.njae.me.uk/2022/12/08/advent-of-code-2022-day-8/
import AoC
-- import Data.Char
text <- readFile dataFileName
let forest = fmap (fmap readTree) $ lines text
-- print forest
- -- print $ findVisibilityOrient forest
- -- print $ findVisibilityForest forest
- -- print $ countVisible $ findVisibilityForest forest
+ -- print $ setVisibilityOrient forest
+ -- print $ setVisibilityForest forest
+ -- print $ countVisible $ setVisibilityForest forest
print $ part1 forest
print $ part2 forest
-- print $ part1 sizedTree
-- print $ part2 sizedTree
-part1 :: Forest -> Int
-part1 = countVisible . findVisibilityForest
--- part1, part2 :: STree -> Integer
--- part1 = foldTree (\x xs -> sum (x:xs)) . fmap cancelLarge
+part1, part2 :: Forest -> Int
+part1 = countVisible . setVisibilityForest
+
+part2 forest = maximum scores
+ where nrows = length forest
+ ncols = length $ head forest
+ scores = [scenicScore forest r c | r <- [0 .. (nrows - 1)], c <- [0 .. (ncols - 1)]]
+
readTree :: Char -> Tree
readTree h = Tree (read [h]) False
-findVisibility :: [Tree] -> [Tree]
-findVisibility row = snd $ foldl' vis (-1, []) row
+isVisible :: Tree -> Bool
+isVisible (Tree _ v) = v
+
+treeHeight :: Tree -> Int
+treeHeight (Tree h _) = h
+
+
+setVisibility :: [Tree] -> [Tree]
+setVisibility row = reverse $ snd $ foldl' vis (-1, []) row
where vis (highest, tagged) (Tree height visible)
- | height > highest = (height, tagged ++ [Tree height True])
- | otherwise = (highest, tagged ++ [Tree height visible])
+ | height > highest = (height, (Tree height True) : tagged)
+ | otherwise = (highest, (Tree height visible) : tagged)
-findVisibilityOrient :: Forest -> Forest
-findVisibilityOrient = fmap findVisibility
+setVisibilityOrient :: Forest -> Forest
+setVisibilityOrient = fmap setVisibility
-findVisibilityForest :: Forest -> Forest
-findVisibilityForest forest = foldl' f forest [1..4]
- where f trees _ = findVisibilityOrient (rotate trees)
+setVisibilityForest :: Forest -> Forest
+setVisibilityForest forest = (!!4) $ iterate f forest
+ where f = rotate . setVisibilityOrient
rotate = (fmap reverse) . transpose
countVisible :: Forest -> Int
-countVisible forest = length $ filter isVisible $ foldl' (++) [] forest
+countVisible forest = length $ filter isVisible $ concat forest
-isVisible :: Tree -> Bool
-isVisible (Tree _ v) = v
-treeHeight :: Tree -> Int
-treeHeight (Tree h _) = h
-
-part2 :: Forest -> Int
-part2 forest = maximum scores
- where nrows = length forest
- ncols = length $ head forest
- scores = [scenicScore forest r c | r <- [0 .. (nrows - 1)], c <- [0 .. (ncols - 1)]]
viewDistance :: Int -> [Tree] -> Int
-viewDistance h trees = length $ takeUntil (< h) $ fmap treeHeight trees
+viewDistance h trees = length $ takeWhile1 (< h) $ fmap treeHeight trees
-takeUntil :: (a -> Bool) -> [a] -> [a]
-takeUntil f [] = []
-takeUntil f (x:xs)
- | f x == True = x : (takeUntil f xs)
+takeWhile1 :: (a -> Bool) -> [a] -> [a]
+takeWhile1 _ [] = []
+takeWhile1 f (x:xs)
+ | f x == True = x : (takeWhile1 f xs)
| otherwise = [x]
tracks :: Forest -> Int -> Int -> [[Tree]]
tracks forest row col = [reverse l, drop 1 r, reverse u, drop 1 d]
where (l, r) = splitAt col (forest !! row)
- -- r = drop 1 r'
(u, d) = splitAt row ((transpose forest) !! col)
- -- d = drop 1 d'
scenicScore :: Forest -> Int -> Int -> Int
scenicScore forest row col = foldl' (*) 1 $ fmap (viewDistance h) directions