Skip to content

Instantly share code, notes, and snippets.

-- Which bodies do not orbit anything?
roots :: Eq a => [(a,a)] -> [a]
roots orbits = [x | (x, y) <- orbits, notElem x . fmap snd $ orbits]
-- 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])
orbits :: [(String, String)]
unfoldFunction :: String -> (String, [String])
unfoldFunction = forestBuilder orbits
main = do
contents <- fmap lines . readFile $ "input.txt"
let orbits = fmap parseOrbit contents
let myForest = unfoldForest (forestBuilder orbits) (roots orbits)
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)