-
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.
Merge pull request #93 from ephemient/hs/day13
Day 13: Claw Contraption
- Loading branch information
Showing
7 changed files
with
87 additions
and
3 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
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,40 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day13 | ||
-- Description: <https://adventofcode.com/2024/day/13 Day 13: Claw Contraption> | ||
module Day13 (part1, part2) where | ||
|
||
import Data.Maybe (mapMaybe) | ||
import Data.String (IsString) | ||
import Data.Text (Text) | ||
import Data.Void (Void) | ||
import Text.Megaparsec (MonadParsec, ParseErrorBundle, Stream (Token, Tokens), parse, sepEndBy) | ||
import Text.Megaparsec.Char (newline, string) | ||
import Text.Megaparsec.Char.Lexer qualified as L (decimal) | ||
|
||
parser :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m [(a, a, a, a, a, a)] | ||
parser = | ||
flip sepEndBy newline $ | ||
(,,,,,) | ||
<$> (string "Button A: X+" *> L.decimal) | ||
<*> (string ", Y+" *> L.decimal <* newline) | ||
<*> (string "Button B: X+" *> L.decimal) | ||
<*> (string ", Y+" *> L.decimal <* newline) | ||
<*> (string "Prize: X=" *> L.decimal) | ||
<*> (string ", Y=" *> L.decimal <* newline) | ||
|
||
solve :: (Integral a) => (a, a, a, a, a, a) -> Maybe a | ||
solve (ax, ay, bx, by_, x, y) | ||
| (a, 0) <- (x * by_ - y * bx) `divMod` (ax * by_ - bx * ay), | ||
(b, 0) <- (x * ay - y * ax) `divMod` (ay * bx - by_ * ax) = | ||
Just $ 3 * a + b | ||
solve _ = Nothing | ||
|
||
part1 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part1 input = sum . mapMaybe solve <$> parse parser "" input | ||
|
||
part2 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part2 input = sum . mapMaybe solve' <$> parse parser "" input | ||
where | ||
solve' (ax, ay, bx, by_, x, y) = solve (ax, ay, bx, by_, x + 10000000000000, y + 10000000000000) |
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,34 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day13Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day13 (part1) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "Button A: X+94, Y+34", | ||
"Button B: X+22, Y+67", | ||
"Prize: X=8400, Y=5400", | ||
"", | ||
"Button A: X+26, Y+66", | ||
"Button B: X+67, Y+21", | ||
"Prize: X=12748, Y=12176", | ||
"", | ||
"Button A: X+17, Y+86", | ||
"Button B: X+84, Y+37", | ||
"Prize: X=7870, Y=6450", | ||
"", | ||
"Button A: X+69, Y+23", | ||
"Button B: X+27, Y+71", | ||
"Prize: X=18641, Y=10279" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` Right 480 |