-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day4 | ||
-- Description: <https://adventofcode.com/2024/day/4 Day 4: Ceres Search> | ||
module Day4 (part1, part2) where | ||
|
||
import Control.Monad (guard) | ||
import Data.List (tails) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (drop, index, length, lines, reverse, splitOn, takeEnd, transpose, unpack) | ||
|
||
part1 :: Text -> Int | ||
part1 input = sum $ pred . length . T.splitOn "XMAS" <$> gs | ||
where | ||
g = T.lines input | ||
d1 = T.transpose (zipWith T.drop [0 ..] g) ++ T.transpose (zipWith T.takeEnd [0 ..] $ T.reverse <$> g) | ||
d2 = T.transpose (zipWith T.drop [0 ..] $ T.reverse <$> g) ++ T.transpose (zipWith T.takeEnd [0 ..] g) | ||
gs = concat [g, T.reverse <$> g, T.transpose g, T.transpose $ reverse g, d1, T.reverse <$> d1, d2, T.reverse <$> d2] | ||
|
||
part2 :: Text -> Int | ||
part2 input = length $ do | ||
prev : line : next : _ <- tails $ T.lines input | ||
(i, 'A') <- zip [0 ..] $ T.unpack line | ||
guard $ [prev !? pred i, prev !? succ i, next !? pred i, next !? succ i] `elem` ["MMSS", "MSMS", "SMSM", "SSMM"] | ||
where | ||
t !? i | ||
| 0 <= i, i < T.length t = t `T.index` i | ||
| otherwise = '\0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day4Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day4 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "MMMSXXMASM", | ||
"MSAMXMSMSA", | ||
"AMXSXMAAMM", | ||
"MSAMASMSMX", | ||
"XMASAMXAMM", | ||
"XXAMMXXAMA", | ||
"SMSMSASXSS", | ||
"SAXAMASAAA", | ||
"MAMMMXMMMM", | ||
"MXMXAXMASX" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 18 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` 9 |