From 3afccf369faab10caeeea3c812f120a15fefbeaf Mon Sep 17 00:00:00 2001 From: Akshay Mankar Date: Sat, 2 Dec 2023 09:22:52 +0100 Subject: [PATCH] [haskell] Solve Day 2 --- answers.nix | 4 ++ haskell/aoc2023.cabal | 4 +- haskell/app/Main.hs | 5 +- haskell/default.nix | 4 +- haskell/src/Day2.hs | 104 ++++++++++++++++++++++++++++++++++++++++++ input/day2 | 100 ++++++++++++++++++++++++++++++++++++++++ input/day2-eg | 5 ++ 7 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 haskell/src/Day2.hs create mode 100644 input/day2 create mode 100644 input/day2-eg diff --git a/answers.nix b/answers.nix index b6eb801..e25b34f 100644 --- a/answers.nix +++ b/answers.nix @@ -3,4 +3,8 @@ part1 = "53194"; part2 = "54249"; }; + day2 = { + part1 = "2406"; + part2 = "78375"; + }; } diff --git a/haskell/aoc2023.cabal b/haskell/aoc2023.cabal index ce38238..309a896 100644 --- a/haskell/aoc2023.cabal +++ b/haskell/aoc2023.cabal @@ -15,10 +15,12 @@ common warnings library import: warnings - build-depends: base ^>=4.17.2.0 hs-source-dirs: src default-language: GHC2021 + build-depends: base ^>=4.17.2.0 + , containers exposed-modules: Day1 + , Day2 executable aoc2023 import: warnings diff --git a/haskell/app/Main.hs b/haskell/app/Main.hs index fe609c7..68c46cc 100644 --- a/haskell/app/Main.hs +++ b/haskell/app/Main.hs @@ -1,11 +1,14 @@ {-# LANGUAGE LambdaCase #-} + module Main where -import qualified Day1 +import Day1 qualified +import Day2 qualified import System.Environment (getArgs) main :: IO () main = do getArgs >>= \case ["day1"] -> Day1.run + ["day2"] -> Day2.run args -> error $ "Invlaid args: " <> show args diff --git a/haskell/default.nix b/haskell/default.nix index f8e6ac5..214aac8 100644 --- a/haskell/default.nix +++ b/haskell/default.nix @@ -1,11 +1,11 @@ -{ mkDerivation, base, lib }: +{ mkDerivation, base, containers, lib }: mkDerivation { pname = "aoc2023"; version = "0.1.0.0"; src = ./.; isLibrary = true; isExecutable = true; - libraryHaskellDepends = [ base ]; + libraryHaskellDepends = [ base containers ]; executableHaskellDepends = [ base ]; homepage = "https://git.pub.solar/axeman/aoc2023"; description = "Advent of Code 2023"; diff --git a/haskell/src/Day2.hs b/haskell/src/Day2.hs new file mode 100644 index 0000000..a7bd486 --- /dev/null +++ b/haskell/src/Day2.hs @@ -0,0 +1,104 @@ +{-# LANGUAGE OverloadedRecordDot #-} + +module Day2 where + +import Control.Applicative ((<|>)) +import Data.Char (isDigit) +import Data.Functor (($>)) +import Data.IntMap (IntMap) +import Data.IntMap qualified as IntMap +import Data.Maybe (fromMaybe) +import Text.ParserCombinators.ReadP + +run :: IO () +run = do + games <- readInput + putStrLn $ "Part 1 Answer: " <> show (part1 games) + putStrLn $ "Part 2 Answer: " <> show (part2 games) + +maxRed, maxGreen, maxBlue :: Int +maxRed = 12 +maxGreen = 13 +maxBlue = 14 + +part1 :: Games -> Int +part1 = sum . IntMap.keys . IntMap.filter isCompatibleGame + +isCompatibleGame :: [Revelation] -> Bool +isCompatibleGame = all isCompatibleRevelation + +isCompatibleRevelation :: Revelation -> Bool +isCompatibleRevelation revelation = + revelation.red <= maxRed + && revelation.green <= maxGreen + && revelation.blue <= maxBlue + +part2 :: Games -> Int +part2 = sum . IntMap.map power + +power :: [Revelation] -> Int +power rs = + let redPower = maximum $ map red rs + greenPower = maximum $ map green rs + bluePower = maximum $ map blue rs + in redPower * greenPower * bluePower + +type Games = IntMap [Revelation] + +data Revelation = Revelation {red :: Int, green :: Int, blue :: Int} + +fromAssocList :: [(Colour, Int)] -> Revelation +fromAssocList m = + -- A fold is probably faster, but its a list of max 3 elements + Revelation + { red = fromMaybe 0 $ lookup Red m, + green = fromMaybe 0 $ lookup Green m, + blue = fromMaybe 0 $ lookup Blue m + } + +data Colour = Red | Green | Blue + deriving (Eq, Ord, Show) + +readInput :: IO Games +readInput = do + inputStr <- getContents + case readP_to_S (parseGames <* eof) inputStr of + [] -> error "Parser failed" + ((g, _) : _) -> pure g + +parseGameId :: ReadP Int +parseGameId = do + _ <- string "Game " + gameId <- read <$> many1 (satisfy isDigit) + _ <- string ": " + pure gameId + +parseColour :: ReadP Colour +parseColour = + do + string "red" $> Red + <|> (string "green" $> Green) + <|> (string "blue" $> Blue) + +parseColourRevelation :: ReadP (Colour, Int) +parseColourRevelation = do + n <- read <$> many1 (satisfy isDigit) + _ <- skipSpaces + colour <- parseColour + pure (colour, n) + +parseRevelation :: ReadP Revelation +parseRevelation = do + fromAssocList <$> sepBy1 parseColourRevelation (string ", ") + +-- >>> readP_to_S (parseGame <* eof) "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green" +-- [((1,[fromList [(Red,4),(Blue,3)],fromList [(Red,1),(Green,2),(Blue,6)],fromList [(Green,2)]]),"")] +parseGame :: ReadP (Int, [Revelation]) +parseGame = do + gameId <- parseGameId + revelations <- sepBy1 parseRevelation (string "; ") + pure (gameId, revelations) + +parseGames :: ReadP Games +parseGames = + IntMap.fromList <$> sepBy1 parseGame (string "\n") diff --git a/input/day2 b/input/day2 new file mode 100644 index 0000000..db81d01 --- /dev/null +++ b/input/day2 @@ -0,0 +1,100 @@ +Game 1: 2 blue, 4 green; 7 blue, 1 red, 14 green; 5 blue, 13 green, 1 red; 1 red, 7 blue, 11 green +Game 2: 6 blue, 3 green; 4 red, 1 green, 7 blue; 2 green +Game 3: 4 blue, 3 red; 2 blue, 4 red, 7 green; 1 blue, 6 red, 7 green; 5 green, 10 blue; 9 green, 1 blue, 6 red; 8 blue, 1 red, 12 green +Game 4: 15 blue, 4 green, 5 red; 2 red, 2 green, 5 blue; 3 green, 13 blue; 17 blue, 1 green, 5 red +Game 5: 11 green, 4 red, 3 blue; 8 blue, 6 green; 8 green, 2 red, 9 blue; 4 red, 16 blue; 8 blue, 10 red, 6 green; 9 blue, 3 red, 10 green +Game 6: 4 green, 9 red, 2 blue; 7 red, 2 green, 15 blue; 13 red, 2 green, 6 blue; 5 green, 7 blue, 6 red; 19 red, 15 blue, 4 green +Game 7: 12 blue, 5 red; 5 green, 6 blue; 5 red, 15 blue; 5 blue, 5 red, 5 green; 1 green, 11 blue, 2 red +Game 8: 6 red, 11 green; 5 red, 2 blue, 7 green; 7 red, 6 green +Game 9: 5 red, 1 blue, 11 green; 4 green, 1 blue; 8 green, 2 red; 1 green, 2 red, 2 blue; 3 green, 2 red +Game 10: 7 blue, 4 red, 11 green; 13 green, 1 red, 1 blue; 7 blue, 6 green +Game 11: 4 blue, 7 red, 2 green; 1 green, 14 red, 3 blue; 2 green, 5 red, 3 blue +Game 12: 6 green, 6 blue, 1 red; 1 green, 3 red, 2 blue; 2 blue, 6 red, 7 green +Game 13: 6 red, 10 green, 13 blue; 3 red, 12 green, 9 blue; 11 blue, 1 green; 4 red, 3 blue, 13 green; 12 green, 10 blue, 6 red; 13 blue, 3 green, 3 red +Game 14: 8 green, 1 blue, 17 red; 7 green, 11 blue, 19 red; 19 red, 9 blue, 2 green; 8 green, 20 red, 12 blue; 16 red, 3 green, 11 blue +Game 15: 3 red, 1 green, 5 blue; 9 blue, 4 green; 6 blue, 5 green, 9 red +Game 16: 13 blue, 1 red; 2 blue, 2 green; 1 green; 10 blue, 8 red; 4 red, 3 green, 9 blue +Game 17: 10 blue, 2 red; 3 green, 4 red; 6 blue, 1 red, 6 green; 5 green, 7 blue, 5 red +Game 18: 3 red, 1 green; 2 red, 5 blue; 5 blue, 2 red +Game 19: 7 green, 4 blue, 1 red; 1 green, 4 blue, 4 red; 6 blue, 8 green; 4 green, 2 blue, 1 red; 1 red, 1 blue, 2 green +Game 20: 13 green, 1 red, 1 blue; 12 green, 1 blue; 5 green, 1 blue, 2 red; 16 green, 3 red; 2 red, 9 green +Game 21: 8 red, 2 green, 2 blue; 5 red, 3 blue; 2 blue, 5 red, 2 green; 7 blue +Game 22: 9 red, 12 blue, 7 green; 7 red, 13 blue, 4 green; 9 blue, 13 red, 1 green; 3 blue, 4 red, 5 green +Game 23: 7 green, 12 red; 6 red, 7 green, 4 blue; 1 blue, 11 red, 5 green; 4 green, 2 blue, 6 red; 12 green, 6 red, 3 blue +Game 24: 11 red, 4 blue; 9 blue, 6 green, 17 red; 8 green, 2 red; 16 blue, 6 red, 2 green +Game 25: 7 red, 4 blue; 7 blue, 4 green; 10 blue, 4 red, 2 green; 6 green, 4 blue, 1 red; 10 blue, 2 red, 4 green +Game 26: 7 green, 8 red, 6 blue; 5 red, 3 green, 2 blue; 13 blue, 6 green, 5 red; 10 blue, 4 red, 8 green; 2 red, 2 blue, 1 green; 8 blue, 1 green, 4 red +Game 27: 7 green, 3 blue, 13 red; 1 green, 17 red, 1 blue; 16 red, 3 blue, 3 green; 5 green, 3 red, 5 blue; 13 red, 4 green, 8 blue; 6 blue, 2 green, 15 red +Game 28: 8 blue, 5 red, 18 green; 1 green, 6 red; 7 blue, 18 green, 5 red; 16 green, 3 red, 7 blue; 6 blue, 18 green; 8 blue, 8 green, 7 red +Game 29: 4 blue, 1 red; 6 blue, 1 red; 17 blue, 1 green +Game 30: 1 red, 2 green, 5 blue; 2 blue, 7 green, 6 red; 11 blue, 4 red, 2 green; 5 green, 6 blue, 4 red; 5 red, 8 blue, 7 green +Game 31: 10 green, 9 blue; 5 green, 9 blue, 1 red; 1 red, 8 blue +Game 32: 3 red, 5 green; 5 red, 5 blue, 14 green; 2 red, 2 green; 11 green, 3 red, 5 blue +Game 33: 7 blue, 10 green, 8 red; 18 blue, 15 green, 4 red; 6 red, 1 green; 18 blue, 8 red, 11 green +Game 34: 3 green; 2 red, 5 green; 5 blue, 3 green; 3 blue, 5 green, 1 red +Game 35: 1 blue, 5 green, 6 red; 3 green, 2 red, 3 blue; 4 red, 9 blue, 3 green; 1 green, 12 blue, 1 red +Game 36: 14 green, 3 blue, 16 red; 1 green, 2 red, 4 blue; 4 blue, 9 green, 18 red; 4 blue, 4 green, 14 red; 4 blue, 11 green +Game 37: 7 green, 2 blue, 3 red; 8 green, 9 red, 2 blue; 4 blue, 15 green, 18 red +Game 38: 11 red, 1 blue, 6 green; 6 green, 2 blue, 1 red; 6 blue, 17 red, 2 green; 17 red, 9 blue, 3 green; 7 red, 7 blue, 3 green; 3 green, 7 red, 7 blue +Game 39: 1 blue, 2 green; 1 blue, 2 green, 7 red; 1 blue, 4 red, 2 green; 1 blue, 12 red +Game 40: 1 blue, 4 red, 15 green; 12 green, 1 blue, 15 red; 15 red, 8 green +Game 41: 5 blue, 5 green, 1 red; 9 red, 8 green, 9 blue; 10 red, 10 blue, 4 green; 3 blue, 17 red, 3 green; 3 blue, 4 red, 2 green +Game 42: 2 blue, 10 red, 17 green; 6 red, 10 green, 10 blue; 3 blue, 6 green, 8 red; 9 green, 2 blue, 8 red; 13 green, 5 blue; 4 red, 18 green, 11 blue +Game 43: 8 red, 3 blue, 6 green; 2 red, 8 green, 10 blue; 5 blue, 9 red, 9 green; 1 green, 15 red, 8 blue +Game 44: 11 green, 19 red, 14 blue; 1 red, 19 green, 9 blue; 7 green, 8 red, 10 blue; 14 green, 8 blue, 15 red; 7 green, 3 red, 2 blue +Game 45: 4 green, 9 blue, 4 red; 7 blue, 13 green, 2 red; 12 green, 10 blue, 10 red +Game 46: 10 red, 2 green, 1 blue; 10 red, 10 green, 1 blue; 1 blue, 13 green; 1 blue, 2 green, 10 red; 1 blue, 7 red, 11 green; 10 red, 5 green +Game 47: 3 blue, 2 green, 12 red; 5 blue, 7 red; 5 green, 14 red; 12 red, 7 green, 5 blue +Game 48: 5 red, 1 blue, 3 green; 7 red, 8 green, 4 blue; 4 blue, 5 green, 17 red; 1 blue, 12 red +Game 49: 2 green, 7 red, 1 blue; 11 green, 5 red; 4 red, 1 blue, 1 green; 11 green, 1 blue, 7 red +Game 50: 10 red, 3 blue, 6 green; 1 blue, 5 red, 3 green; 6 blue, 11 red, 12 green; 10 green +Game 51: 18 blue, 1 green, 1 red; 15 blue; 13 blue, 11 green, 4 red; 8 red, 1 green, 18 blue; 10 green, 7 blue, 8 red +Game 52: 13 green, 15 blue; 6 blue, 4 red, 8 green; 6 red, 13 green, 11 blue; 2 red, 7 green, 13 blue; 12 green, 2 blue, 3 red; 6 red, 11 green, 1 blue +Game 53: 2 red, 2 green; 3 green, 1 blue, 1 red; 1 blue, 4 green, 7 red; 4 red, 1 blue; 4 red, 5 green, 2 blue +Game 54: 8 blue, 2 red, 5 green; 6 green, 2 blue, 3 red; 1 blue, 8 green, 4 red +Game 55: 6 green, 6 blue, 3 red; 13 green, 1 red; 2 blue, 1 red, 1 green; 14 green, 1 blue, 1 red; 1 blue, 2 red, 9 green; 9 green, 2 blue, 4 red +Game 56: 4 green, 6 blue, 1 red; 5 red, 3 blue; 6 red, 1 blue; 9 green, 5 blue, 7 red +Game 57: 5 red, 5 green, 8 blue; 11 red, 3 blue, 8 green; 7 green, 9 blue, 11 red; 3 green, 2 blue, 12 red +Game 58: 3 green, 3 red; 4 red, 1 green; 1 red, 6 green; 5 green; 5 red, 1 blue, 3 green; 3 red, 1 blue +Game 59: 2 green, 2 blue; 7 red, 18 green; 2 blue, 7 red, 16 green; 7 red, 10 green +Game 60: 3 blue, 4 red; 4 blue, 3 red, 3 green; 16 green +Game 61: 1 blue, 2 red, 8 green; 9 blue, 4 green, 12 red; 10 green, 2 red; 5 blue, 11 red, 1 green; 10 green, 3 blue, 8 red; 5 red, 2 green +Game 62: 15 red, 10 blue, 7 green; 4 blue, 9 red, 4 green; 4 red, 2 blue, 2 green; 11 green, 2 red; 8 blue, 2 green; 2 green, 8 red, 8 blue +Game 63: 2 green, 3 blue, 1 red; 7 blue, 5 red; 7 blue +Game 64: 3 green, 5 blue, 6 red; 9 green, 4 red; 13 red, 1 blue, 5 green; 4 blue, 13 red, 8 green +Game 65: 7 green, 1 blue; 1 red, 14 blue, 4 green; 8 blue, 6 red; 14 green, 4 red +Game 66: 6 red, 11 green, 7 blue; 1 blue, 6 red; 13 red, 7 blue, 3 green; 8 red, 6 blue, 15 green; 7 green, 6 blue, 4 red; 4 red, 1 blue, 20 green +Game 67: 4 blue, 9 green; 15 red, 16 green, 3 blue; 1 green, 14 red, 3 blue; 3 red, 2 blue, 3 green; 4 green, 3 blue, 12 red +Game 68: 5 green, 3 blue, 2 red; 4 green, 8 blue, 11 red; 6 red, 6 blue, 4 green; 8 red, 5 blue, 7 green; 6 blue, 6 green, 11 red; 2 blue, 3 green, 3 red +Game 69: 15 blue, 16 green, 5 red; 10 blue, 3 red, 13 green; 4 red, 5 blue, 2 green; 1 red; 11 green, 5 red, 15 blue +Game 70: 8 red, 9 blue, 12 green; 3 red, 2 blue, 14 green; 10 blue, 1 red, 18 green; 1 blue, 7 red, 16 green; 3 green, 4 red, 16 blue; 10 green, 6 red +Game 71: 12 blue, 7 red, 16 green; 2 red, 9 blue, 15 green; 1 red, 11 blue, 11 green; 15 red, 16 blue, 2 green +Game 72: 1 blue, 11 red, 6 green; 1 red, 2 blue, 5 green; 4 green, 2 red; 2 green, 12 red +Game 73: 1 blue, 1 red; 2 red, 4 blue, 2 green; 1 blue, 2 green, 10 red; 8 red +Game 74: 12 red, 1 green, 4 blue; 1 red, 5 blue, 1 green; 11 green, 16 red, 7 blue; 7 red, 1 blue, 1 green; 12 red, 11 green, 12 blue; 11 green, 6 red +Game 75: 12 green, 8 red, 3 blue; 7 red, 10 green; 1 green, 7 blue, 1 red +Game 76: 4 green, 1 red, 3 blue; 7 blue, 3 green, 3 red; 4 blue, 2 red, 3 green; 4 blue, 1 green +Game 77: 2 green, 12 blue, 10 red; 5 blue, 7 red; 2 red, 6 green; 1 blue, 2 red, 6 green +Game 78: 2 green, 4 blue, 4 red; 8 green, 10 red, 10 blue; 5 green, 8 blue, 10 red; 6 green, 2 red +Game 79: 3 green, 2 blue, 11 red; 8 red, 11 green, 1 blue; 1 blue, 16 red; 5 red, 7 green, 16 blue; 12 red, 7 green, 9 blue; 4 red, 20 blue, 12 green +Game 80: 3 red, 5 green; 2 blue, 4 green; 2 red, 12 green, 4 blue; 10 green, 1 blue, 1 red; 4 blue, 3 red +Game 81: 1 blue, 1 green, 1 red; 5 green, 3 red, 1 blue; 1 blue, 6 green; 1 green; 1 red, 5 green, 2 blue; 1 blue, 1 red, 3 green +Game 82: 7 green, 10 blue, 3 red; 10 green, 12 red, 12 blue; 18 red, 8 green, 14 blue; 3 red, 3 green, 10 blue; 3 red, 1 blue, 5 green; 1 green, 8 blue +Game 83: 9 red, 3 blue; 14 blue, 8 red, 3 green; 14 blue, 5 green, 4 red +Game 84: 2 blue, 3 red, 6 green; 11 green, 2 red, 1 blue; 17 green, 3 blue, 3 red; 1 red, 1 blue; 1 red, 2 blue, 19 green +Game 85: 3 green, 2 blue, 3 red; 4 red, 5 blue, 8 green; 15 green, 1 red, 9 blue; 12 green, 3 blue, 2 red +Game 86: 15 green, 7 red, 10 blue; 2 blue, 2 red, 1 green; 4 red, 1 green, 9 blue; 7 red, 14 blue, 5 green +Game 87: 1 green, 3 blue, 1 red; 2 blue, 1 green; 1 blue, 2 green, 1 red +Game 88: 2 green, 6 blue, 5 red; 5 blue, 2 red; 3 red, 13 blue; 9 blue, 10 red, 1 green +Game 89: 6 green, 10 red, 2 blue; 7 red, 1 blue, 8 green; 4 blue, 3 red, 5 green; 4 green, 4 blue, 10 red +Game 90: 8 red, 7 blue; 4 green, 3 red, 1 blue; 5 blue, 2 green +Game 91: 15 green, 14 red; 12 red, 16 green, 2 blue; 8 red, 10 green; 1 green, 6 red; 8 green, 12 red +Game 92: 4 blue, 4 green, 9 red; 1 blue, 17 green; 1 green; 15 green, 3 blue, 12 red; 11 red, 1 blue, 7 green; 7 blue, 13 red, 8 green +Game 93: 10 blue, 12 red; 10 blue, 11 green, 8 red; 1 blue, 11 green, 7 red; 10 blue, 15 red, 5 green; 11 red, 8 green, 9 blue; 10 green, 3 blue +Game 94: 1 blue, 2 red; 4 red, 1 green, 5 blue; 3 red, 2 green; 2 green, 2 blue; 1 red, 5 blue, 1 green; 4 blue, 1 red, 2 green +Game 95: 1 red, 1 blue, 3 green; 2 green, 6 blue; 1 green, 13 blue, 1 red; 3 green, 15 blue +Game 96: 16 blue, 7 green, 5 red; 5 green, 5 blue, 6 red; 3 green, 17 blue, 10 red; 13 blue, 2 red, 1 green +Game 97: 12 red; 1 blue, 6 red, 1 green; 9 red, 2 blue, 1 green; 1 green, 2 blue, 1 red; 15 red, 1 blue; 1 blue +Game 98: 11 red, 6 blue, 13 green; 4 blue, 2 red, 12 green; 2 blue, 8 green, 10 red +Game 99: 2 red, 1 blue; 4 green; 7 green, 1 blue, 1 red; 5 green, 2 red; 1 blue, 2 red, 9 green; 2 green, 3 red +Game 100: 7 red, 11 blue; 10 red, 5 blue, 1 green; 7 red, 1 green, 13 blue; 9 red; 9 red, 19 blue; 9 red, 9 blue \ No newline at end of file diff --git a/input/day2-eg b/input/day2-eg new file mode 100644 index 0000000..1cd7d33 --- /dev/null +++ b/input/day2-eg @@ -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 \ No newline at end of file