diff --git a/hs/src/Day4.hs b/hs/src/Day4.hs index 5d620d0..83023bf 100644 --- a/hs/src/Day4.hs +++ b/hs/src/Day4.hs @@ -5,20 +5,20 @@ -- Description: module Day4 (part1, part2) where -import Control.Monad (ap, guard) -import Data.Function (on) +import Control.Monad (guard) import Data.List (tails) import Data.Text (Text) import Data.Text qualified as T (drop, lines, reverse, splitAt, transpose, unpack, zip) import Data.Text.Internal.Search qualified as T (indices) part1 :: Text -> Int -part1 input = sum $ ((+) `on` length . T.indices "XMAS") `ap` T.reverse <$> grid ++ T.transpose grid ++ concat diagonals +part1 input = sum $ countXmas <$> grid ++ T.transpose grid ++ concat diagonals where grid = T.lines input diagonals = do (lower, upper) <- unzip . zipWith T.splitAt [0 ..] <$> [grid, reverse grid] T.transpose <$> [T.reverse <$> lower, upper] + countXmas line = length (T.indices "XMAS" line) + length (T.indices "SAMX" line) part2 :: Text -> Int part2 input = length $ do