2023 day 12: star 1
This commit is contained in:
parent
e3a9d35260
commit
5ad9a42a5f
22
2023/12/Main1.hs
Normal file
22
2023/12/Main1.hs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
allSprings :: String -> [String]
|
||||||
|
allSprings = foldr (liftA2 (:) . getChars) [""]
|
||||||
|
where
|
||||||
|
getChars c = if c == '?' then ['.', '#'] else [c]
|
||||||
|
|
||||||
|
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"
|
Loading…
Reference in a new issue