diff --git a/Main.hs b/Main.hs index 7c4fb85..b04a007 100644 --- a/Main.hs +++ b/Main.hs @@ -1,9 +1,14 @@ +{-# OPTIONS_GHC -Wno-unused-imports #-} + module Main where import Day1 +import Day2 main :: IO () main = do putStrLn "AoC 2023" - putStrLn "Day 1" - Day1.main + --putStrLn "Day 1" + --Day1.main + putStrLn "Day 2" + Day2.main diff --git a/inputs/day2-test.input b/inputs/day2-test.input new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/inputs/day2-test.input @@ -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/inputs/day2.input b/inputs/day2.input new file mode 100644 index 0000000..61950d0 --- /dev/null +++ b/inputs/day2.input @@ -0,0 +1,100 @@ +Game 1: 10 green, 5 blue; 1 red, 9 green, 10 blue; 5 blue, 6 green, 2 red; 7 green, 9 blue, 1 red; 2 red, 10 blue, 10 green; 7 blue, 1 red +Game 2: 7 green, 5 red, 3 blue; 4 blue, 7 green, 8 red; 9 blue, 4 green; 6 green, 3 red, 4 blue +Game 3: 2 green, 4 blue, 13 red; 15 blue, 9 red, 3 green; 3 red, 18 blue, 3 green; 6 red, 4 green, 2 blue; 6 blue, 13 red +Game 4: 9 red, 1 green, 13 blue; 3 red; 2 blue, 6 red, 1 green; 12 blue, 2 red +Game 5: 1 red, 8 green; 2 red, 8 green, 8 blue; 1 red, 11 green; 5 blue, 11 green; 11 blue, 2 green; 10 blue, 2 red, 1 green +Game 6: 1 red, 12 blue; 20 blue, 3 green, 2 red; 4 red, 15 blue +Game 7: 13 red, 9 green, 1 blue; 8 green, 2 red, 6 blue; 4 green, 5 blue; 7 red, 3 green, 7 blue; 19 red, 5 blue, 1 green +Game 8: 11 red, 14 green, 4 blue; 2 blue, 5 green, 16 red; 18 blue, 11 red, 2 green; 2 blue, 15 red; 13 green, 8 blue +Game 9: 7 green, 5 blue, 11 red; 10 red, 7 green, 4 blue; 1 red; 6 green, 2 blue, 9 red; 8 green, 10 red, 6 blue; 5 red, 5 green, 7 blue +Game 10: 4 blue, 2 green, 1 red; 5 green, 2 red, 1 blue; 3 green, 8 blue, 1 red; 2 blue, 6 green, 2 red; 1 red, 4 green, 2 blue +Game 11: 3 red, 4 blue; 8 blue, 7 green, 2 red; 7 blue, 1 red, 6 green; 13 blue, 4 green +Game 12: 2 red, 3 blue, 4 green; 2 blue, 9 red, 8 green; 10 red, 1 blue; 1 green, 7 red, 3 blue; 7 red, 2 blue, 9 green +Game 13: 12 red, 6 green, 2 blue; 15 green, 2 red, 4 blue; 7 green, 1 red, 3 blue +Game 14: 9 green, 4 red; 6 blue, 1 red, 7 green; 3 blue, 5 green +Game 15: 7 red, 3 green, 2 blue; 3 blue, 4 green; 4 blue, 4 green, 9 red +Game 16: 12 blue, 11 green, 4 red; 8 blue, 9 red, 10 green; 9 green, 11 blue, 13 red; 10 red, 5 blue, 6 green; 2 red; 2 blue, 5 green, 5 red +Game 17: 3 red, 2 green, 2 blue; 1 blue, 3 red, 1 green; 10 green +Game 18: 3 green, 1 blue, 4 red; 12 red, 5 green; 3 red, 3 green, 3 blue; 13 red, 2 blue +Game 19: 13 blue, 8 green, 6 red; 10 red, 12 blue; 8 green, 13 red, 9 blue; 13 green, 3 red, 5 blue; 5 green, 1 blue, 2 red +Game 20: 19 red, 13 blue, 4 green; 1 red, 4 green, 8 blue; 14 red, 6 blue, 7 green; 11 red, 13 blue, 8 green +Game 21: 3 green, 13 red, 7 blue; 1 blue, 1 green, 1 red; 3 blue, 15 red, 5 green; 3 blue, 15 red, 2 green; 6 green, 9 red, 14 blue +Game 22: 2 red, 6 green, 4 blue; 6 green, 2 red; 1 blue, 4 red, 3 green; 11 green, 7 blue, 1 red; 4 red, 8 green, 3 blue +Game 23: 14 blue; 3 green, 2 red, 3 blue; 5 blue, 1 red +Game 24: 12 red; 5 blue, 16 red; 2 blue, 1 green, 16 red; 1 green, 11 red; 2 blue, 8 red, 1 green +Game 25: 4 red, 11 blue, 1 green; 7 red, 9 blue; 6 blue, 10 green +Game 26: 3 green, 13 red; 7 blue, 13 red, 5 green; 5 blue, 8 green, 11 red; 7 blue, 18 green, 6 red +Game 27: 6 green, 6 red, 5 blue; 2 blue, 4 green, 11 red; 15 red, 6 green; 4 green, 12 red, 2 blue; 3 blue, 5 red +Game 28: 16 blue, 6 red, 1 green; 7 red, 4 green, 10 blue; 1 red, 4 green +Game 29: 5 blue, 4 red; 6 blue, 3 red, 4 green; 2 green, 4 red, 5 blue; 1 green, 7 blue, 4 red; 3 green, 2 blue, 4 red +Game 30: 2 green; 14 green, 1 blue, 2 red; 5 red, 14 green +Game 31: 9 blue, 6 red, 7 green; 20 red, 1 green, 15 blue; 6 blue, 7 green, 17 red; 2 blue, 3 green, 6 red; 1 red, 3 blue, 2 green; 5 green, 18 red, 6 blue +Game 32: 7 green, 9 blue, 8 red; 8 red, 13 green, 19 blue; 2 red, 9 blue, 3 green; 9 green, 6 blue, 6 red +Game 33: 6 blue, 12 red; 13 blue, 3 green, 15 red; 5 red, 10 blue, 4 green; 11 blue, 6 red +Game 34: 5 green, 16 blue, 6 red; 10 green, 1 blue, 4 red; 2 red, 7 blue, 6 green; 12 green, 4 blue, 4 red +Game 35: 11 green, 3 blue; 1 red, 6 blue, 10 green; 11 green, 3 blue; 1 red, 2 blue; 11 green, 3 blue, 1 red; 3 blue, 2 green +Game 36: 10 green, 6 red, 4 blue; 3 green, 3 blue, 5 red; 6 red, 5 blue, 10 green +Game 37: 8 red, 7 blue, 5 green; 8 blue, 5 green, 14 red; 8 red, 2 blue +Game 38: 4 green, 1 red, 4 blue; 8 green, 11 blue; 7 red, 5 blue +Game 39: 2 blue, 4 red, 4 green; 8 green, 8 red, 1 blue; 3 red +Game 40: 4 green, 3 red, 14 blue; 4 blue, 13 green, 3 red; 12 green, 2 red, 2 blue; 8 green, 1 red, 11 blue; 4 green, 1 red, 1 blue +Game 41: 4 red, 1 green, 2 blue; 4 red; 4 red +Game 42: 12 red, 8 blue, 1 green; 8 green, 6 red, 5 blue; 12 green, 3 red, 13 blue; 1 red, 2 green, 8 blue; 3 green, 5 red, 6 blue +Game 43: 12 green, 3 blue; 13 green, 7 red, 5 blue; 10 green, 4 red, 4 blue +Game 44: 6 green, 2 red, 4 blue; 10 green, 6 red; 5 blue, 15 red, 13 green; 1 blue, 6 red, 3 green; 9 red, 5 green, 3 blue; 6 green, 4 blue, 5 red +Game 45: 10 blue, 14 green; 2 green, 2 red, 12 blue; 7 green, 1 red; 8 blue, 6 green, 1 red +Game 46: 8 red, 10 green, 15 blue; 9 green, 3 red, 17 blue; 2 blue, 10 red, 5 green; 11 blue, 3 green, 9 red; 5 red, 11 blue, 1 green; 7 green, 5 red, 16 blue +Game 47: 10 blue, 1 green, 1 red; 3 red, 8 blue, 7 green; 8 red, 9 blue; 2 green, 8 red, 1 blue +Game 48: 5 blue, 2 green; 2 red, 7 green, 2 blue; 1 blue, 3 green, 1 red +Game 49: 2 green, 6 red, 5 blue; 6 green; 4 blue, 17 red, 5 green +Game 50: 5 blue, 10 green; 6 blue, 4 red, 9 green; 7 red, 4 blue; 7 red, 3 blue, 14 green; 5 blue, 10 green, 9 red; 13 green, 1 blue, 9 red +Game 51: 1 blue, 15 green; 6 green, 2 blue; 5 blue, 1 red, 12 green +Game 52: 3 red, 15 green; 7 blue, 1 red, 14 green; 8 green, 1 red, 12 blue; 1 red, 9 green, 7 blue +Game 53: 2 green, 4 red; 1 red, 1 blue; 3 blue, 1 green; 2 red, 2 blue, 2 green +Game 54: 7 blue, 13 red, 7 green; 1 red, 2 green; 11 red, 10 green, 5 blue; 10 red, 8 green, 5 blue; 8 green, 12 blue, 12 red +Game 55: 18 red, 3 green, 5 blue; 5 green, 3 blue, 7 red; 3 blue, 3 green, 4 red +Game 56: 14 red, 17 green, 2 blue; 5 green, 13 red, 1 blue; 11 red, 20 green +Game 57: 3 red, 6 green, 2 blue; 3 red, 2 green; 2 green, 5 red; 1 blue, 1 green, 2 red +Game 58: 7 blue, 5 green, 9 red; 10 red, 5 green, 9 blue; 2 blue, 3 red, 8 green; 8 blue, 9 red; 7 red, 3 blue, 7 green; 2 green, 7 red, 1 blue +Game 59: 4 green, 3 blue; 10 red, 4 green, 4 blue; 2 green, 14 red, 12 blue; 1 blue, 1 green, 13 red; 10 red, 3 green, 3 blue; 2 green +Game 60: 9 red, 13 blue; 2 green, 5 red, 9 blue; 3 green, 10 blue +Game 61: 2 red, 8 green, 4 blue; 3 green, 2 red; 10 red, 9 green, 12 blue; 11 green, 17 blue, 3 red; 7 green, 1 red, 14 blue +Game 62: 1 green, 5 red, 13 blue; 5 blue, 1 green, 8 red; 2 green, 8 blue, 3 red; 1 green, 8 red +Game 63: 8 green, 15 red, 2 blue; 4 blue, 3 red, 12 green; 4 green, 1 blue, 17 red; 9 green, 18 red, 4 blue +Game 64: 7 blue, 17 red, 17 green; 3 blue, 4 green, 3 red; 4 red, 19 green, 1 blue; 11 blue, 14 red; 4 blue, 19 green, 7 red; 1 red, 10 green, 11 blue +Game 65: 1 blue, 17 red, 5 green; 17 red, 3 blue, 2 green; 10 blue, 9 green +Game 66: 5 blue, 6 red; 8 red, 2 blue, 1 green; 2 green, 3 blue; 8 blue, 10 red; 1 green, 2 red, 5 blue; 1 red, 3 blue +Game 67: 12 green, 16 blue, 12 red; 15 red, 1 blue, 3 green; 10 red, 3 green, 10 blue; 2 blue, 6 green, 6 red; 9 red, 8 blue, 7 green +Game 68: 10 red, 7 blue; 12 blue, 9 red; 12 blue, 9 red, 2 green +Game 69: 14 blue, 3 red, 3 green; 7 green, 7 red, 2 blue; 8 blue, 4 green, 8 red; 6 blue, 14 red, 3 green +Game 70: 7 blue, 6 green, 2 red; 2 red, 4 blue, 4 green; 2 red, 5 blue, 3 green; 6 green, 2 blue; 5 blue, 2 red, 2 green +Game 71: 7 green, 15 blue, 3 red; 15 blue, 15 red, 2 green; 10 red, 9 blue; 6 green, 20 blue, 11 red; 12 blue, 3 green, 7 red; 1 red, 7 blue +Game 72: 2 green, 9 blue, 7 red; 5 green, 3 blue, 5 red; 10 blue, 8 red, 7 green +Game 73: 18 blue, 5 red, 1 green; 18 blue, 3 red, 9 green; 2 red, 4 blue, 9 green; 5 blue, 5 red; 2 blue, 10 green, 6 red +Game 74: 1 blue, 10 green, 5 red; 4 green, 12 blue, 6 red; 7 red, 13 green, 3 blue; 5 blue, 8 green, 4 red +Game 75: 4 red, 2 blue, 5 green; 2 blue, 7 red, 4 green; 2 blue, 4 green, 3 red; 12 green, 2 blue; 10 green, 1 blue, 2 red +Game 76: 8 green, 6 blue, 5 red; 1 red, 2 blue, 9 green; 7 red, 9 green; 5 green, 1 blue, 11 red +Game 77: 3 blue, 10 red, 9 green; 7 blue, 6 red, 4 green; 4 red, 1 green, 8 blue +Game 78: 2 blue, 1 red, 14 green; 11 green, 1 blue; 15 green, 1 red +Game 79: 3 green, 17 blue, 1 red; 3 red, 2 blue, 10 green; 13 blue, 11 green, 5 red; 16 blue, 2 green, 16 red; 11 green, 1 blue, 14 red +Game 80: 7 red, 10 blue, 5 green; 6 blue, 6 green, 8 red; 6 blue, 3 green, 5 red +Game 81: 1 blue, 14 red, 6 green; 1 red, 13 blue, 12 green; 2 green, 15 red, 15 blue +Game 82: 5 blue, 8 red, 6 green; 19 blue, 4 green; 9 green, 15 blue, 3 red +Game 83: 19 red, 15 green, 2 blue; 17 red, 4 green, 1 blue; 13 green, 18 red +Game 84: 9 green, 14 red; 11 green, 14 red, 1 blue; 1 blue, 2 red, 3 green; 13 green, 10 red; 1 green, 5 red +Game 85: 4 red, 2 green, 11 blue; 8 blue, 3 red; 4 red, 1 blue, 5 green; 2 red, 3 green; 1 green, 8 red, 12 blue +Game 86: 5 blue, 1 red; 8 blue; 2 red, 1 green, 12 blue; 12 blue, 2 red +Game 87: 3 red, 10 green, 3 blue; 13 blue, 6 red, 2 green; 1 green, 2 red, 10 blue +Game 88: 10 red, 3 green, 8 blue; 3 red, 18 blue, 2 green; 3 green, 15 blue; 15 green, 16 blue, 8 red +Game 89: 10 blue, 1 red; 4 green, 9 red, 13 blue; 10 red, 3 green, 12 blue; 2 green, 1 red, 16 blue; 10 blue, 1 red, 6 green +Game 90: 4 red, 2 blue, 15 green; 5 red, 1 blue, 12 green; 3 blue, 3 red, 7 green; 4 blue, 3 red, 7 green; 1 red, 2 green, 1 blue; 1 blue, 4 green, 3 red +Game 91: 16 red, 10 blue, 1 green; 13 green, 13 red, 19 blue; 11 blue, 12 green, 2 red +Game 92: 8 blue, 4 green, 5 red; 7 blue, 4 red; 2 green, 15 blue; 16 blue, 4 red; 1 red, 7 green, 16 blue; 11 blue, 1 red, 3 green +Game 93: 12 green, 2 blue, 2 red; 8 red, 16 green, 8 blue; 15 red, 4 blue, 7 green; 1 red, 4 blue, 15 green; 13 green, 5 red, 4 blue; 5 green, 8 blue, 12 red +Game 94: 13 green, 10 red; 11 red, 19 green, 1 blue; 1 blue, 10 red, 12 green; 18 green, 9 red, 1 blue; 8 green, 1 red +Game 95: 3 green, 4 blue; 2 red, 2 green, 2 blue; 7 red, 3 green +Game 96: 5 red, 7 green; 4 blue, 14 green, 10 red; 13 green; 13 green, 3 blue; 13 green, 1 red, 3 blue; 12 red, 1 green +Game 97: 2 green, 1 blue; 9 red; 4 blue, 8 red; 4 green, 1 red, 14 blue; 2 green, 9 blue; 1 red, 6 blue, 2 green +Game 98: 12 green, 9 blue, 13 red; 6 red, 7 blue; 2 blue, 2 green +Game 99: 9 red, 3 green, 10 blue; 10 red, 10 blue, 4 green; 2 green, 15 blue, 3 red; 12 blue, 4 red +Game 100: 15 blue, 6 red; 1 green, 2 red; 12 blue, 8 green, 1 red; 1 red, 7 blue diff --git a/package.yaml b/package.yaml index 055dde9..72f1c33 100644 --- a/package.yaml +++ b/package.yaml @@ -20,4 +20,5 @@ library: source-dirs: src exposed-modules: - Day1 + - Day2 diff --git a/src/Day2.hs b/src/Day2.hs new file mode 100644 index 0000000..c5f16a2 --- /dev/null +++ b/src/Day2.hs @@ -0,0 +1,120 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-unused-do-bind #-} + +module Day2 where + +-- import qualified Data.ByteString as B +-- import qualified Data.ByteString.Char8 as Char8 +import Data.Attoparsec.ByteString.Char8 as A +import Data.Either (fromRight) +import Data.String (fromString) +import Data.Maybe + +-- import Debug.Trace + +main :: IO () +main = do + dayInput <- readFile "inputs/day2.input" + putStrLn "Part 1 result:" + print $ part1 dayInput + putStrLn "Part 2 result:" + print $ part2 dayInput + +data GameSpec = + GameSpec { gameSpecId :: Int, gameSpecInstances :: [GameInstance] } + deriving (Show) + +data GameInstance = GameInstance + { redDice :: Maybe Int + , greenDice :: Maybe Int + , blueDice :: Maybe Int + } + deriving (Show, Ord, Eq) + +data Dice = RedDice Int + | GreenDice Int + | BlueDice Int + deriving Show + +-- Returns a *new* maximal game instance, and not necessarily an existing game +-- instance +maxCubes :: [GameInstance] -> GameInstance +maxCubes gix = GameInstance (maxRed gix) (maxGreen gix) (maxBlue gix) + where + maxRed = maximum . map redDice + maxGreen = maximum . map greenDice + maxBlue = maximum . map blueDice + +-- Determine which games would have been possible if the bag had been loaded +-- with only 12 red cubes, 13 green cubes, and 14 blue cubes. +part1 :: String -> String +part1 = show + . sum + . mapMaybe (possibleGame1 (GameInstance (Just 12) (Just 13) (Just 14))) + . parseInput + +possibleGame1 :: GameInstance -> GameSpec -> Maybe Int +possibleGame1 spec actual = + if maxCubes [spec, maxCubes $ gameSpecInstances actual] == spec + then Just $ gameSpecId actual + else Nothing + +part2 :: String -> String +part2 = show + . sum + . map possibleGame2 + . parseInput + +-- Fewest number of cubes of each color +possibleGame2 :: GameSpec -> Int +possibleGame2 actual = red * green * blue + where + maxInstance = maxCubes $ gameSpecInstances actual + red = fromMaybe 0 $ redDice maxInstance + green = fromMaybe 0 $ greenDice maxInstance + blue = fromMaybe 0 $ blueDice maxInstance + +parseInput :: String -> [GameSpec] +parseInput = fromRight [] . parseOnly input . fromString + where + input :: Parser [GameSpec] + input = line `sepBy` endOfLine + + -- Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red + line :: Parser GameSpec + line = GameSpec <$> gameId <*> gameInstances + + gameId :: Parser Int + gameId = "Game" *> space *> decimal <* ": " + + gameInstances :: Parser [GameInstance] + gameInstances = sepBy gameInstance "; " + + gameInstance :: Parser GameInstance + gameInstance = + sortThatShit <$> sepBy1 + ( choice [ RedDice <$> (decimal :: Parser Int) <* space <* "red" + , GreenDice <$> (decimal :: Parser Int) <* space <* "green" + , BlueDice <$> (decimal :: Parser Int) <* space <* "blue" + ] :: Parser Dice + ) ", " + where + sortThatShit :: [Dice] -> GameInstance + sortThatShit dx = GameInstance + (lookup "red" axs) + (lookup "green" axs) + (lookup "blue" axs) + where + axs = map toPair dx + toPair :: Dice -> (String,Int) + toPair (RedDice i) = ("red", i) + toPair (GreenDice i) = ("green", i) + toPair (BlueDice i) = ("blue", i) + --pure $ GameInstance Nothing Nothing (Just blue) + --where + -- parseRed = (decimal :: Parser Int) <* space <* "red" + -- parseGreen = (decimal :: Parser Int) <* space <* "green" + -- parseBlue = (decimal :: Parser Int) <* space <* "blue" + + +