Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. module AoCDay8Part1 where
  2.  
  3. data Node = Node [Node] [Int]
  4.  
  5. solve :: [Int] -> ([Int], Node)
  6. solve (nChild:nEntries:rest) =
  7. (remainingToBeParsed, Node children entries)
  8. where
  9. remainingToBeParsed = drop nEntries restChildren
  10. entries = take nEntries restChildren
  11. (restChildren, children) = makeChildren nChild (rest, [])
  12.  
  13. makeChildren :: Int -> ([Int], [Node]) -> ([Int], [Node])
  14. makeChildren n (remaining, children)
  15. | n == length children = (remaining, children)
  16. | otherwise = makeChildren n (next, nextChild:children)
  17. where (next, nextChild) = solve remaining
  18.  
  19. sumEntries :: Node -> Int
  20. sumEntries (Node children entries)
  21. | length children == 0 = sum entries
  22. | otherwise = sum (entries <> map sumEntries children)
  23.  
  24. testInput = "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2"
  25.  
  26. main :: IO ()
  27. main =
  28. print $ sumEntries $ snd $ solve $ read <$> words testInput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement