Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module AoCDay8Part1 where
- data Node = Node [Node] [Int]
- solve :: [Int] -> ([Int], Node)
- solve (nChild:nEntries:rest) =
- (remainingToBeParsed, Node children entries)
- where
- remainingToBeParsed = drop nEntries restChildren
- entries = take nEntries restChildren
- (restChildren, children) = makeChildren nChild (rest, [])
- makeChildren :: Int -> ([Int], [Node]) -> ([Int], [Node])
- makeChildren n (remaining, children)
- | n == length children = (remaining, children)
- | otherwise = makeChildren n (next, nextChild:children)
- where (next, nextChild) = solve remaining
- sumEntries :: Node -> Int
- sumEntries (Node children entries)
- | length children == 0 = sum entries
- | otherwise = sum (entries <> map sumEntries children)
- testInput = "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2"
- main :: IO ()
- main =
- print $ sumEntries $ snd $ solve $ read <$> words testInput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement