advent-of-code/2024/4/Main1.hs
2024-12-03 23:19:47 -08:00

24 lines
687 B
Haskell

import Data.List (isPrefixOf, transpose)
import Data.Universe.Helpers (diagonals)
countTimes :: String -> String -> Int
countTimes _ [] = 0
countTimes s fullLine@(_:nextLine)
| s `isPrefixOf` fullLine = 1 + countTimes s (drop (length s) fullLine)
| otherwise = countTimes s nextLine
countHoriz :: String -> Int
countHoriz l = countTimes "XMAS" l + countTimes "SAMX" l
countLines :: [String] -> Int
countLines = sum . map countHoriz
countAll :: [String] -> Int
countAll ls = countLines ls
+ countLines (transpose ls)
+ countLines (diagonals ls)
+ countLines (diagonals (reverse ls))
main :: IO ()
main = print . countAll . lines =<< readFile "data.txt"