2023 day 12: star 1
This commit is contained in:
parent
e3a9d35260
commit
c1a0cdff1e
25
2023/12/Main1.hs
Normal file
25
2023/12/Main1.hs
Normal file
|
@ -0,0 +1,25 @@
|
|||
import Data.List.Split (splitOn)
|
||||
import Data.List (group)
|
||||
import Control.Applicative (Applicative(liftA2))
|
||||
import Data.Bool (bool)
|
||||
|
||||
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"
|
Loading…
Reference in a new issue