This commit is contained in:
2024-12-05 15:33:14 +01:00
parent ed2e74ba45
commit e4c38318e9
5 changed files with 47 additions and 7 deletions

View File

@ -9,12 +9,12 @@ 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
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
then 1 + countSubstrings' word rest
else countSubstrings' word rest
main = do
contents <- lines <$> readFile "day4.txt"

View File

@ -10,8 +10,8 @@ countSubstrings word text = sum (map (countSubstrings' word) diags) + sum (map (
where diags = diagonals text
countSubstrings' _ [] = 0
countSubstrings' word text@(_:rest) = if word `isPrefixOf` text
then 1 + countSubstrings' word rest
else countSubstrings' word rest
then 1 + countSubstrings' word rest
else countSubstrings' word rest
submatricesVert :: Int -> [String] -> [[String]]
submatricesVert _ [] = []