blob: 6dbf8a02fc01585ca4951cee291cd14c709adc34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
{-# LANGUAGE OverloadedStrings #-}
import Data.Char
import Data.Maybe
import Data.Word
import Data.Scientific
import qualified Data.Aeson as JSON
import qualified Data.Vector as Vector
import qualified Data.HashMap.Strict as HM
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Internal as BS (c2w,w2c)
contains :: (Eq a) => [a] -> a -> Bool
contains [] _ = False
contains (x:xs) v = if x == v then True else contains xs v
isDigitOrMinus :: Char -> Bool
isDigitOrMinus c = isDigit c || c == '-'
extractNumbers :: String -> [Int]
extractNumbers s@(x:xs)
| isDigitOrMinus x = read (takeWhile isDigitOrMinus s) : extractNumbers (dropWhile isDigitOrMinus xs)
| otherwise = extractNumbers xs
extractNumbers [] = []
sumnumsnored :: JSON.Value -> Int
sumnumsnored (JSON.Object o) = if any (== JSON.String "red") $ HM.elems o then 0 else HM.foldl' (\a v -> a + sumnumsnored v) 0 o
sumnumsnored (JSON.Array a) = Vector.sum $ Vector.map sumnumsnored a
sumnumsnored (JSON.String _) = 0
sumnumsnored (JSON.Number n) = floor n
sumnumsnored (JSON.Bool _) = 0
day12 :: IO ()
day12 = do
input <- readFile "day12.txt"
print $ sum $ extractNumbers input
dopart2 :: String -> Int
dopart2 s = sumnumsnored $ fromJust $ JSON.decode $ BS.pack $ map BS.c2w s
day12_2 :: IO ()
day12_2 = do
input <- readFile "day12.txt"
print $ dopart2 input
main = day12_2
|