Skip to content

Commit

Permalink
use isSubmapOfBy
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 2, 2023
1 parent 54a4252 commit ad16c1f
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions solutions/src/2023/02.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ main :: IO ()
main =
do input <- [format|2023 2 (Game %d: (%d %s)&(, )&(; )%n)*|]
let summaries = [(i, summarizeGame rounds) | (i, rounds) <- input]
print (sum [i | (i, summary) <- summaries, all part1 (Map.assocs summary)])
print (sum [i | (i, summary) <- summaries, Map.isSubmapOfBy (<=) summary part1])
print (sum [product m | (_, m) <- summaries])

-- | Find the minimum marbles needed to play a whole game
Expand All @@ -51,9 +51,6 @@ summarizeGame = Map.unionsWith max . map summarizeRound
summarizeRound :: [(Int, String)] -> Map String Int
summarizeRound r = Map.fromListWith (+) [(color, count) | (count, color) <- r]

-- | Check if the number of marbles of a certain color is below the threshold for part 1
part1 :: (String, Int) -> Bool
part1 ("red" , v) = v <= 12
part1 ("green", v) = v <= 13
part1 ("blue" , v) = v <= 14
part1 _ = False
-- | Marbles available in part 1
part1 :: Map String Int
part1 = Map.fromList [("red", 12), ("green", 13), ("blue", 14)]

0 comments on commit ad16c1f

Please sign in to comment.