Day 4, Part 1

This commit is contained in:
daniele 2024-12-04 21:29:44 +01:00
parent 77a259a438
commit 6f4398b6d0
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514

21
Day4/puzzle1.hs Normal file
View File

@ -0,0 +1,21 @@
import Data.List (transpose, isPrefixOf)
diagonals :: [String] -> [String]
diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs)
where diagonals' xs = transpose (zipWith drop [0..] xs)
++ transpose (zipWith drop [1..] (transpose xs))
countSubstrings :: String -> [String] -> Int
countSubstrings word text = sum (map (countSubstrings' word) text) + sum (map (countSubstrings' word . reverse) text)
+ sum (map (countSubstrings' word) cols) + sum (map (countSubstrings' word . reverse) cols)
+ sum (map (countSubstrings' word) diags) + sum (map (countSubstrings' word . reverse) diags)
where cols = transpose text
diags = diagonals text
countSubstrings' _ [] = 0
countSubstrings' word text@(_:rest) = if word `isPrefixOf` text
then 1 + countSubstrings' word rest
else countSubstrings' word rest
main = do
contents <- lines <$> readFile "day4.txt"
print $ countSubstrings "XMAS" contents