-
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
7 changed files
with
66 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 @@ | ||
8 | ||
2286 |
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,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 |
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,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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.