Skip to content

Commit

Permalink
day 2 is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Javran committed Jan 7, 2024
1 parent 07912fb commit 85daf85
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions advent-of-code.cabal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions data/testdata/2023/day/2/example.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8
2286
5 changes: 5 additions & 0 deletions data/testdata/2023/day/2/example.input.txt
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
1 change: 1 addition & 0 deletions src/Javran/AdventOfCode/Solutions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions src/Javran/AdventOfCode/Y2023/Day2.hs
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
2 changes: 1 addition & 1 deletion test/Javran/AdventOfCode/TestdataSpec.hs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85daf85

Please sign in to comment.