Add 2015 day 12 part2

This commit is contained in:
Xavier Morel
2020-12-16 17:28:41 +01:00
parent 1301cd1a8e
commit b0ef0e7aab
2 changed files with 29 additions and 7 deletions

View File

@@ -20,15 +20,19 @@ extra-source-files:
description: Please see the README on GitHub at <https://github.com/mx42/haskellAoC#readme> description: Please see the README on GitHub at <https://github.com/mx42/haskellAoC#readme>
dependencies: dependencies:
- aeson
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- containers - containers
- directory - directory
- matrix - matrix
- memoize - memoize
- pureMD5 - pureMD5
- scientific
- sort - sort
- split - split
- unordered-containers
- utf8-string - utf8-string
- vector
library: library:
source-dirs: src source-dirs: src

View File

@@ -1,12 +1,13 @@
module Y2015.Day12 (y15day12) where 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 import Data.Char
import Data.Maybe
-- getToNext :: Char -> String -> String import Data.ByteString.Lazy.UTF8 (fromString)
-- getToNext _ [] = [] import Debug.Trace
-- getToNext c (x:xs)
-- | x == c = []
-- | otherwise = x:(getToNext c xs)
getStructureSum :: String -> Int getStructureSum :: String -> Int
getStructureSum s = sum $ map read $ words $ map keepNum s 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 | isDigit c = c
| otherwise = ' ' | 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 :: [String] -> (String, String)
y15day12 (input:_) = (part1, part2) y15day12 (input:_) = (part1, part2)
where part1 = show $ getStructureSum input where part1 = show $ getStructureSum input
part2 = show $ "WIP" part2 = show $ getJsonSum 0 $ fromJust jsonData
jsonData = JSON.decode $ fromString input :: Maybe JSON.Value