diff --git a/Day4/puzzle1.hs b/Day4/puzzle1.hs index 4db2f38..2c8f336 100644 --- a/Day4/puzzle1.hs +++ b/Day4/puzzle1.hs @@ -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 diff --git a/Day4/puzzle2.hs b/Day4/puzzle2.hs index 0d02298..b770b8c 100644 --- a/Day4/puzzle2.hs +++ b/Day4/puzzle2.hs @@ -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