27 lines
795 B
Haskell
Executable File
27 lines
795 B
Haskell
Executable File
#! /usr/bin/env stack
|
|
-- stack --resolver lts-12.20 --install-ghc script
|
|
{-# LANGUAGE DeriveFunctor, DeriveFoldable #-}
|
|
module Main where
|
|
|
|
import qualified Control.Monad.State as St
|
|
import qualified Data.List as L
|
|
import qualified Data.Monoid as Mo
|
|
|
|
data Node a = Node [Node a] a deriving (Show, Functor, Foldable)
|
|
|
|
main :: IO ()
|
|
main = interact $ show . Mo.getSum . foldMap (foldMap Mo.Sum) . parseTree
|
|
|
|
parseTree :: String -> Node [Int]
|
|
parseTree = St.evalState parseNode
|
|
|
|
parseNode :: St.State String (Node [Int])
|
|
parseNode = do
|
|
nChildren <- parseInt
|
|
nMetadata <- parseInt
|
|
Node <$> St.replicateM nChildren parseNode
|
|
<*> St.replicateM nMetadata parseInt
|
|
|
|
parseInt :: St.State String Int
|
|
parseInt = read <$> St.state (L.break (== ' ') . L.dropWhile (== ' '))
|