diff --git a/README.md b/README.md index da9b41f6..887baff7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Solutions, scripting, and templates - all in one repo. | `<++++++++>` | `<++++++++>` | `<++++++++>` | `<++++++++>` | `<++++++++>` | | :-: | :-: | :-: | :-: | :-: | -| ☑ [Day 1](src/Javran/AdventOfCode/Y2023/Day1.hs) | | | | | +| ☑ [Day 1](src/Javran/AdventOfCode/Y2023/Day1.hs) | ☐ [Day 2](src/Javran/AdventOfCode/Y2023/Day2.hs) | | | | ### 2022 diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 8f81859c..7d790470 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -253,6 +253,7 @@ library Javran.AdventOfCode.Y2022.Day8 Javran.AdventOfCode.Y2022.Day9 Javran.AdventOfCode.Y2023.Day1 + Javran.AdventOfCode.Y2023.Day2 hs-source-dirs: src default-extensions: diff --git a/data/testdata/2023/day/2/example.expect.txt b/data/testdata/2023/day/2/example.expect.txt new file mode 100644 index 00000000..dbfdaf93 --- /dev/null +++ b/data/testdata/2023/day/2/example.expect.txt @@ -0,0 +1,2 @@ +8 +2286 diff --git a/data/testdata/2023/day/2/example.input.txt b/data/testdata/2023/day/2/example.input.txt new file mode 100644 index 00000000..295c36dd --- /dev/null +++ b/data/testdata/2023/day/2/example.input.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green diff --git a/src/Javran/AdventOfCode/Solutions.hs b/src/Javran/AdventOfCode/Solutions.hs index 9a57028e..0dab0794 100644 --- a/src/Javran/AdventOfCode/Solutions.hs +++ b/src/Javran/AdventOfCode/Solutions.hs @@ -212,6 +212,7 @@ import Javran.AdventOfCode.Y2022.Day23 () import Javran.AdventOfCode.Y2022.Day24 () import Javran.AdventOfCode.Y2022.Day25 () import Javran.AdventOfCode.Y2023.Day1 () +import Javran.AdventOfCode.Y2023.Day2 () {- ORMOLU_ENABLE -} allSolutions :: IM.IntMap (IM.IntMap SomeSolution) diff --git a/src/Javran/AdventOfCode/Y2023/Day2.hs b/src/Javran/AdventOfCode/Y2023/Day2.hs new file mode 100644 index 00000000..a02a41ee --- /dev/null +++ b/src/Javran/AdventOfCode/Y2023/Day2.hs @@ -0,0 +1,55 @@ +{-# LANGUAGE PatternSynonyms #-} + +module Javran.AdventOfCode.Y2023.Day2 () where + +import Data.Monoid hiding (First, Last) +import Data.Semigroup +import Javran.AdventOfCode.Prelude +import Text.ParserCombinators.ReadP hiding (count, get, many) + +data Day2 deriving (Generic) + +newtype Rgb a = Rgb (a, a, a) + deriving stock (Show) + deriving (Semigroup) via (Sum a, Sum a, Sum a) + deriving (Monoid) via (Sum a, Sum a, Sum a) + +type GameLine = [Rgb Int] + +oneSetP :: ReadP (Rgb Int) +oneSetP = + mconcat <$> do + numAndCubeP `sepBy1` strP ", " + where + numAndCubeP = do + n <- decimal1P + strP " " + (Rgb (n, 0, 0) <$ strP "red") + <++ (Rgb (0, n, 0) <$ strP "green") + <++ (Rgb (0, 0, n) <$ strP "blue") + +gameLineP :: ReadP (Int, GameLine) +gameLineP = do + strP "Game " + n <- decimal1P @Int + strP ": " + (n,) <$> oneSetP `sepBy1` strP "; " + +{- + It would be neat if this sort of thing can be put in some general library instead of us having to + declare it here - which makes it a bit more too verbose to my liking. + -} +pattern MaxRgb :: a -> a -> a -> (Max a, Max a, Max a) +pattern MaxRgb r g b = (Max r, Max g, Max b) + +findPower :: GameLine -> Int +findPower = (\(MaxRgb r g b) -> r * g * b) . foldMap (\(Rgb (r, g, b)) -> MaxRgb r g b) + +instance Solution Day2 where + solutionSolved _ = True + solutionRun _ SolutionContext {getInputS, answerShow} = do + xs <- fmap (consumeOrDie gameLineP) . lines <$> getInputS + answerShow $ + let isPossible (Rgb (r, g, b)) = r <= 12 && g <= 13 && b <= 14 + in getSum $ foldMap (\(which, ys) -> if all isPossible ys then Sum which else 0) xs + answerShow $ getSum $ foldMap (Sum . findPower . snd) xs diff --git a/test/Javran/AdventOfCode/TestdataSpec.hs b/test/Javran/AdventOfCode/TestdataSpec.hs index 1abb9407..bd273c36 100644 --- a/test/Javran/AdventOfCode/TestdataSpec.hs +++ b/test/Javran/AdventOfCode/TestdataSpec.hs @@ -34,7 +34,7 @@ import Test.Hspec {- ORMOLU_DISABLE -} hashForForceRecompliation :: String -- FORCE_RECOMP_HASH_BEGIN -hashForForceRecompliation = "a0e89f9f94963cf3144cc38f22281111640cb60d64c8db1f54e46c2b463b3d3a" +hashForForceRecompliation = "431f1ccc5de7897c90c43ca59c56baf6898b8f9eb9675752579d81f3b1a979c7" -- FORCE_RECOMP_HASH_END {- ORMOLU_ENABLE -}