This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foldable t => (a -> a -> a) -> t a -> Maybe a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Forest a = [Tree a] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parseOrbit :: String -> (String, String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Which bodies do not orbit anything? | |
roots :: Eq a => [(a,a)] -> [a] | |
roots orbits = [x | (x, y) <- orbits, notElem x . fmap snd $ orbits] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Given the orbit data, what orbits a given body? | |
forestBuilder :: Eq a => [(a,a)] -> a -> (a,[a]) | |
forestBuilder orbits body = (body, [snd orbit | orbit <- orbits, body == fst orbit]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
orbits :: [(String, String)] | |
unfoldFunction :: String -> (String, [String]) | |
unfoldFunction = forestBuilder orbits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main = do | |
contents <- fmap lines . readFile $ "input.txt" | |
let orbits = fmap parseOrbit contents | |
let myForest = unfoldForest (forestBuilder orbits) (roots orbits) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foldTree :: (a -> [b] -> b) -> Tree a -> b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
levels :: Tree a -> [[a]] | |
sumDepthsTree :: Integral b => Tree a -> b | |
sumDepthsTree = snd . foldl foldFunc (0, 0) . levels | |
foldFunc :: Integral a => (a,a) -> [b] -> (a,a) | |
foldFunc (depth, acc) x = (depth + 1, acc + depth * genericLength x) |