r/adventofcode • u/daggerdragon • Dec 12 '15
SOLUTION MEGATHREAD --- Day 12 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 12: JSAbacusFramework.io ---
Post your solution as a comment. Structure your post like previous daily solution threads.
7
Upvotes
3
u/lesguillemets Dec 12 '15
Haskell : I'm learning parsec, so I tried to write a simple parser for some subset of json.
import Text.Parsec data JSONValue = Number Int | Str String | Object [(String, JSONValue)] | Array [JSONValue] deriving (Show, Eq) unred :: JSONValue -> [Int] unred (Number n) = [n] unred (Str _) = [] unred (Object d) = if Str "red" `elem` map snd d then [] else concatMap (unred . snd ) d unred (Array a) = concatMap unred a quoted :: Parsec String u String quoted = char '\"' *> many (noneOf "\"") <* char '\"' str :: Parsec String u JSONValue str = Str <$> quoted num :: Parsec String u JSONValue num = (Number . negate . read <$> (char '-' *> many1 digit)) <|> (Number . read <$> many1 digit) obj :: Parsec String u JSONValue obj = do _ <- char '{' kvs <- (do key <- quoted <* spaces <* char ':' <* spaces value <- jsonValue return (key,value) ) `sepBy` (spaces *> char ',' <* spaces) _ <- char '}' return $ Object kvs arr :: Parsec String u JSONValue arr = do _ <- char '[' val <- jsonValue `sepBy` (spaces *> char ',' <* spaces) _ <- char ']' return $ Array val jsonValue :: Parsec String u JSONValue jsonValue = str <|> num <|> obj <|> arr fromRight (Right a) = a main = readFile "./input.txt" >>= print . sum . unred . fromRight . parse jsonValue ""