2023 day 12: star 1

This commit is contained in:
eriedaberrie 2023-12-11 22:33:07 -08:00
parent e3a9d35260
commit 8d68c25894

24
2023/12/Main1.hs Normal file
View file

@ -0,0 +1,24 @@
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 :: [Int] -> String -> Int
solve records = length . filter (matchesConsecutive records) . 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"