From: Neil Smith Date: Thu, 8 Dec 2022 20:17:18 +0000 (+0200) Subject: Tidying X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-22.git;a=commitdiff_plain;h=49b6bfbbdd44d081f8fcf8cf952ce5c3dc8a5f27 Tidying --- diff --git a/advent08/Main.hs b/advent08/Main.hs index 12f0b21..e822fc2 100644 --- a/advent08/Main.hs +++ b/advent08/Main.hs @@ -1,4 +1,4 @@ --- 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 @@ -15,66 +15,65 @@ main = 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