Rename function
This commit is contained in:
parent
f9d219a18d
commit
3e9c9c18aa
@ -5,17 +5,17 @@ diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs)
|
|||||||
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
||||||
++ transpose (zipWith drop [1..] (transpose xs))
|
++ transpose (zipWith drop [1..] (transpose xs))
|
||||||
|
|
||||||
countSubstrings :: String -> [String] -> Int
|
countOccurrences :: String -> [String] -> Int
|
||||||
countSubstrings word text = sum (map (countSubstrings' word) text) + sum (map (countSubstrings' word . reverse) text)
|
countOccurrences word text = sum (map (countOccurrences' word) text) + sum (map (countOccurrences' word . reverse) text)
|
||||||
+ sum (map (countSubstrings' word) cols) + sum (map (countSubstrings' word . reverse) cols)
|
+ sum (map (countOccurrences' word) cols) + sum (map (countOccurrences' word . reverse) cols)
|
||||||
+ sum (map (countSubstrings' word) diags) + sum (map (countSubstrings' word . reverse) diags)
|
+ sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags)
|
||||||
where cols = transpose text
|
where cols = transpose text
|
||||||
diags = diagonals text
|
diags = diagonals text
|
||||||
countSubstrings' _ [] = 0
|
countOccurrences' _ [] = 0
|
||||||
countSubstrings' word text@(_:rest) = if word `isPrefixOf` text
|
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
||||||
then 1 + countSubstrings' word rest
|
then 1 + countOccurrences' word rest
|
||||||
else countSubstrings' word rest
|
else countOccurrences' word rest
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
contents <- lines <$> readFile "day4.txt"
|
contents <- lines <$> readFile "day4.txt"
|
||||||
print $ countSubstrings "XMAS" contents
|
print $ countOccurrences "XMAS" contents
|
||||||
|
@ -5,13 +5,13 @@ diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs)
|
|||||||
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
||||||
++ transpose (zipWith drop [1..] (transpose xs))
|
++ transpose (zipWith drop [1..] (transpose xs))
|
||||||
|
|
||||||
countSubstrings :: String -> [String] -> Int
|
countOccurrences :: String -> [String] -> Int
|
||||||
countSubstrings word text = sum (map (countSubstrings' word) diags) + sum (map (countSubstrings' word . reverse) diags)
|
countOccurrences word text = sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags)
|
||||||
where diags = diagonals text
|
where diags = diagonals text
|
||||||
countSubstrings' _ [] = 0
|
countOccurrences' _ [] = 0
|
||||||
countSubstrings' word text@(_:rest) = if word `isPrefixOf` text
|
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
||||||
then 1 + countSubstrings' word rest
|
then 1 + countOccurrences' word rest
|
||||||
else countSubstrings' word rest
|
else countOccurrences' word rest
|
||||||
|
|
||||||
submatricesVert :: Int -> [String] -> [[String]]
|
submatricesVert :: Int -> [String] -> [[String]]
|
||||||
submatricesVert _ [] = []
|
submatricesVert _ [] = []
|
||||||
@ -22,5 +22,5 @@ submatricesVert n matrix@(xs:xxs) = submatrix matrix ++ submatricesVert n xxs
|
|||||||
|
|
||||||
main = do
|
main = do
|
||||||
contents <- lines <$> readFile "day4.txt"
|
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
|
print xmas
|
||||||
|
Loading…
x
Reference in New Issue
Block a user