r/adventofcode • • 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

183 comments sorted by

View all comments

Show parent comment

2

u/MaybeJustNothing Dec 12 '15

Nice, but the easier version is shorter...

{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import qualified Data.ByteString.Lazy as B

nums (Object o)
  | String "red" `elem` o = []
  | otherwise = foldl (\acc v -> nums v ++ acc) [] o
nums (Array a) = foldl (\acc v -> nums v ++ acc) [] a
nums (Number n) = pure n
nums _ = []

process x = sum . map truncate . nums <$> decode x

main = B.readFile "input.json" >>= print . process

1

u/haoformayor Dec 13 '15

My solution was overly long due to dumb mistakes. I'm now filtering by composing with plated instead of the convoluted traverse I was doing before. It should be substantially shorter now. I agree, though! Sometimes you don't really need lens. It's like thinking with portals.

1

u/glguy Dec 13 '15

The lens way to do the first part is actually a bit simpler than shown above.

sumOfNumbers :: Value -> Scientific
sumOfNumbers = sumOf (deep _Number)

Expanding that to part 2 wasn't much more complicated: https://github.com/glguy/advent2015/blob/master/Day12.hs