advent-of-code/2023/12/Main1.hs

25 lines
716 B
Haskell
Raw Normal View History

2023-12-11 22:33:07 -08:00
import Data.List.Split (splitOn)
import Data.List (group)
import Control.Applicative (liftA2)
matchesConsecutive :: [Int] -> String -> Bool
matchesConsecutive xs = (==) xs . map length . filter ((==) '#' . head) . group
getChars :: Char -> [Char]
getChars '?' = ['.', '#']
getChars c = [c]
allSprings :: String -> [String]
allSprings = foldr (liftA2 (:) . getChars) [""]
solve :: (String, [Int]) -> Int
solve (springs, records) = length . filter (matchesConsecutive records) . allSprings $ springs
parseLine :: String -> (String, [Int])
parseLine l = (x, map read . splitOn "," $ y)
where
(x:y:_) = splitOn " " l
main :: IO ()
main = print . sum . map (solve . parseLine) . lines =<< readFile "data.txt"