feat(day4): Implement part2

This commit is contained in:
kalmenn 2024-12-09 01:42:39 +01:00
parent f15fef050d
commit 101634cb2b
Signed by: kalmenn
GPG key ID: F500055C44BC3834

View file

@ -44,6 +44,33 @@ part1 fp = do
Left e -> fail $ "Oh no! " ++ show e
Right v -> return v
windows3 :: [a] -> [(a, a, a)]
windows3 (a:b:c:tl) = (a, b, c) : windows3 (b:c:tl)
windows3 _ = []
windows3x3 :: [[a]] -> [((a, a, a), (a, a, a), (a, a, a))]
windows3x3 ls = [(wa, wb, wc)
| (a, b, c) <- windows3 ls
, (wa, wb, wc) <- zip3 (windows3 a) (windows3 b) (windows3 c)]
countCrossMAS :: [String] -> Int
countCrossMAS = sum
. map (\((tl, _, tr),
(_ , c, _ ),
(bl, _, br)) ->
if isMAS [tl, c, br] && isMAS [tr, c, bl] then 1 else 0)
. windows3x3
where
isMAS :: String -> Bool
isMAS = (||) <$> (==) "MAS" <*> (==) "SAM"
part2 :: FilePath -> IO Int
part2 fp = do
content <- readFile fp
case parse rows fp content of
Left e -> fail $ "Oh no! " ++ show e
Right r -> return $ countCrossMAS r
main :: IO ()
main = do
args <- getArgs