From c693f1d6ef3e59db4ef84382bb94c12d1cc9a326 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Mon, 11 Dec 2023 22:33:07 -0800 Subject: [PATCH] 2023 day 12: star 1 --- 2023/12/Main1.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2023/12/Main1.hs diff --git a/2023/12/Main1.hs b/2023/12/Main1.hs new file mode 100644 index 0000000..7a1beee --- /dev/null +++ b/2023/12/Main1.hs @@ -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 :: (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) . filter (not . null) . lines =<< readFile "data.txt"