Rename function

This commit is contained in:
daniele 2024-12-05 22:50:14 +01:00
parent f9d219a18d
commit 3e9c9c18aa
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
2 changed files with 16 additions and 16 deletions

View File

@ -5,17 +5,17 @@ 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)
countOccurrences :: String -> [String] -> Int
countOccurrences word text = sum (map (countOccurrences' word) text) + sum (map (countOccurrences' word . reverse) text)
+ sum (map (countOccurrences' word) cols) + sum (map (countOccurrences' word . reverse) cols)
+ sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' 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
countOccurrences' _ [] = 0
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
then 1 + countOccurrences' word rest
else countOccurrences' word rest
main = do
contents <- lines <$> readFile "day4.txt"
print $ countSubstrings "XMAS" contents
print $ countOccurrences "XMAS" contents

View File

@ -5,13 +5,13 @@ 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) diags) + sum (map (countSubstrings' word . reverse) diags)
countOccurrences :: String -> [String] -> Int
countOccurrences word text = sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags)
where diags = diagonals text
countSubstrings' _ [] = 0
countSubstrings' word text@(_:rest) = if word `isPrefixOf` text
then 1 + countSubstrings' word rest
else countSubstrings' word rest
countOccurrences' _ [] = 0
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
then 1 + countOccurrences' word rest
else countOccurrences' word rest
submatricesVert :: Int -> [String] -> [[String]]
submatricesVert _ [] = []
@ -22,5 +22,5 @@ submatricesVert n matrix@(xs:xxs) = submatrix matrix ++ submatricesVert n xxs
main = do
contents <- lines <$> readFile "day4.txt"
let xmas = length . filter (\x -> countSubstrings "MAS" x == 2) . concatMap (submatricesVert 3) . transpose $ map tails contents
let xmas = length . filter (\x -> countOccurrences "MAS" x == 2) . concatMap (submatricesVert 3) . transpose $ map tails contents
print xmas