mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 13:59:51 +01:00
Add 2015 day 12 part2
This commit is contained in:
@@ -20,15 +20,19 @@ extra-source-files:
|
||||
description: Please see the README on GitHub at <https://github.com/mx42/haskellAoC#readme>
|
||||
|
||||
dependencies:
|
||||
- aeson
|
||||
- base >= 4.7 && < 5
|
||||
- containers
|
||||
- directory
|
||||
- matrix
|
||||
- memoize
|
||||
- pureMD5
|
||||
- scientific
|
||||
- sort
|
||||
- split
|
||||
- unordered-containers
|
||||
- utf8-string
|
||||
- vector
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user