diff --git a/haskellAoC/package.yaml b/haskellAoC/package.yaml index 52040cf..d89207b 100644 --- a/haskellAoC/package.yaml +++ b/haskellAoC/package.yaml @@ -20,15 +20,19 @@ extra-source-files: description: Please see the README on GitHub at dependencies: +- aeson - base >= 4.7 && < 5 - containers - directory - matrix - memoize - pureMD5 +- scientific - sort - split +- unordered-containers - utf8-string +- vector library: source-dirs: src diff --git a/haskellAoC/src/Y2015/Day12.hs b/haskellAoC/src/Y2015/Day12.hs index ca41b6e..05437fd 100644 --- a/haskellAoC/src/Y2015/Day12.hs +++ b/haskellAoC/src/Y2015/Day12.hs @@ -1,12 +1,13 @@ module Y2015.Day12 (y15day12) where +import qualified Data.Aeson as JSON +import qualified Data.Vector as Vector +import qualified Data.HashMap.Strict as HMap +import qualified Data.Scientific as S import Data.Char - --- getToNext :: Char -> String -> String --- getToNext _ [] = [] --- getToNext c (x:xs) --- | x == c = [] --- | otherwise = x:(getToNext c xs) +import Data.Maybe +import Data.ByteString.Lazy.UTF8 (fromString) +import Debug.Trace getStructureSum :: String -> Int getStructureSum s = sum $ map read $ words $ map keepNum s @@ -15,7 +16,24 @@ getStructureSum s = sum $ map read $ words $ map keepNum s | isDigit c = c | otherwise = ' ' +getJsonSum :: Int -> JSON.Value -> Int +getJsonSum n (JSON.Object o) = + case isRed of + True -> n + False -> HMap.foldl' getJsonSum n o + where isRed = any red values + values = HMap.elems o + red (JSON.String s) = (== "\"red\"") $ show s + red _ = False +getJsonSum n (JSON.Array a) = Vector.foldl getJsonSum n a +getJsonSum n (JSON.Number nb) = n + intval + where intval = case (S.toBoundedInteger nb :: Maybe Int) of + Just nb' -> nb' + Nothing -> 0 +getJsonSum n _ = n + y15day12 :: [String] -> (String, String) y15day12 (input:_) = (part1, part2) where part1 = show $ getStructureSum input - part2 = show $ "WIP" + part2 = show $ getJsonSum 0 $ fromJust jsonData + jsonData = JSON.decode $ fromString input :: Maybe JSON.Value