Skip to content

Instantly share code, notes, and snippets.

@ntreu14
Last active October 7, 2019 12:56
Show Gist options
  • Save ntreu14/c4eeae70993e6ef6dad32cf26af42c27 to your computer and use it in GitHub Desktop.
Save ntreu14/c4eeae70993e6ef6dad32cf26af42c27 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment