projects
/
advent-of-code-22.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
19047d8
)
Tidying
author
Neil Smith
<NeilNjae@users.noreply.github.com>
Thu, 8 Dec 2022 20:17:18 +0000
(22:17 +0200)
committer
Neil Smith
<NeilNjae@users.noreply.github.com>
Thu, 8 Dec 2022 20:17:18 +0000
(22:17 +0200)
advent08/Main.hs
patch
|
blob
|
history
diff --git
a/advent08/Main.hs
b/advent08/Main.hs
index 12f0b214f9f904b10930047d045b98bf733b22ad..e822fc278098418b6809c3b9fc9e1978a452de1d 100644
(file)
--- a/
advent08/Main.hs
+++ b/
advent08/Main.hs
@@
-1,4
+1,4
@@
--- Writeup at https://work.njae.me.uk/2022/12/0
7/advent-of-code-2022-day-7
/
+-- Writeup at https://work.njae.me.uk/2022/12/0
8/advent-of-code-2022-day-8
/
import AoC
-- import Data.Char
import AoC
-- import Data.Char
@@
-15,66
+15,65
@@
main =
text <- readFile dataFileName
let forest = fmap (fmap readTree) $ lines text
-- print forest
text <- readFile dataFileName
let forest = fmap (fmap readTree) $ lines text
-- print forest
- -- print $
find
VisibilityOrient forest
- -- print $
find
VisibilityForest forest
- -- print $ countVisible $
find
VisibilityForest forest
+ -- print $
set
VisibilityOrient forest
+ -- print $
set
VisibilityForest forest
+ -- print $ countVisible $
set
VisibilityForest forest
print $ part1 forest
print $ part2 forest
-- print $ part1 sizedTree
-- print $ part2 sizedTree
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
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)
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
)
-
find
VisibilityOrient :: Forest -> Forest
-
findVisibilityOrient = fmap find
Visibility
+
set
VisibilityOrient :: Forest -> Forest
+
setVisibilityOrient = fmap set
Visibility
-
find
VisibilityForest :: Forest -> Forest
-findVisibilityForest forest = foldl' f forest [1..4]
- where f
trees _ = findVisibilityOrient (rotate trees)
+
set
VisibilityForest :: Forest -> Forest
+setVisibilityForest forest = (!!4) $ iterate f forest
+ where f
= rotate . setVisibilityOrient
rotate = (fmap reverse) . transpose
countVisible :: Forest -> Int
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 :: Int -> [Tree] -> Int
-viewDistance h trees = length $ take
Until
(< h) $ fmap treeHeight trees
+viewDistance h trees = length $ take
While1
(< h) $ fmap treeHeight trees
-take
Until
:: (a -> Bool) -> [a] -> [a]
-take
Until f
[] = []
-take
Until
f (x:xs)
- | f x == True = x : (take
Until
f xs)
+take
While1
:: (a -> Bool) -> [a] -> [a]
+take
While1 _
[] = []
+take
While1
f (x:xs)
+ | f x == True = x : (take
While1
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)
| 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)
(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
scenicScore :: Forest -> Int -> Int -> Int
scenicScore forest row col = foldl' (*) 1 $ fmap (viewDistance h) directions