2023-10-09 20:44:52 +02:00
|
|
|
module Day01 (parse, solve1, solve2) where
|
|
|
|
|
|
|
|
import Data.Char (isPrint)
|
2024-07-30 01:11:49 +02:00
|
|
|
import Data.List (findIndex, scanl)
|
2023-10-09 20:44:52 +02:00
|
|
|
import Data.Maybe (fromJust)
|
|
|
|
|
|
|
|
parse :: String -> String
|
|
|
|
parse = filter isPrint
|
|
|
|
|
|
|
|
mapper :: Char -> Int
|
|
|
|
mapper c = case c of
|
|
|
|
'(' -> 1
|
|
|
|
')' -> -1
|
|
|
|
_ -> undefined
|
|
|
|
|
|
|
|
solve1 :: String -> Int
|
|
|
|
solve1 = sum . map mapper
|
|
|
|
|
|
|
|
solve2 :: String -> Int
|
2024-07-30 01:11:49 +02:00
|
|
|
solve2 = fromJust . findIndex (< 0) . scanl reducer 0
|
|
|
|
where
|
|
|
|
reducer acc next = acc + mapper next
|