diff --git a/2023/12/Main1.hs b/2023/12/Main1.hs new file mode 100644 index 0000000..78c40c6 --- /dev/null +++ b/2023/12/Main1.hs @@ -0,0 +1,24 @@ +import Control.Applicative (liftA2) +import Data.List (group) +import Data.List.Split (splitOn) + +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"