advent-of-code/2015/haskell/solutions/Day01.hs
2024-07-30 01:11:49 +02:00

23 lines
446 B
Haskell

module Day01 (parse, solve1, solve2) where
import Data.Char (isPrint)
import Data.List (findIndex, scanl)
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
solve2 = fromJust . findIndex (< 0) . scanl reducer 0
where
reducer acc next = acc + mapper next