advent-of-code/2023/12/Main1.hs
2023-12-12 20:25:25 -08:00

25 lines
700 B
Haskell

import Control.Applicative (liftA2)
import Data.List (group)
import Data.List.Split (splitOn)
getConsecutives :: String -> [Int]
getConsecutives = map length . filter ((== '#') . head) . group
allSprings :: String -> [String]
allSprings = foldr (liftA2 (:) . getChars) [""]
where
getChars '?' = ['.', '#']
getChars c = [c]
solve :: [Int] -> String -> Int
solve records = length . filter ((== records) . getConsecutives) . allSprings
parseLine :: String -> ([Int], String)
parseLine l = (map read . splitOn "," $ y, x)
where
(x:y:_) = splitOn " " l
main :: IO ()
main = print . sum . map (uncurry solve . parseLine) . lines
=<< readFile "data.txt"