1+ import functools
2+ import math
3+ import operator
14import re
25
36from adventofcode .util .exceptions import SolutionNotFoundError
@@ -34,6 +37,18 @@ def check_game(constraints: dict[str, int], game_values: list[dict[str, int]]) -
3437 return True
3538
3639
40+ def check_minimum (game_values : list [dict [str , int ]]) -> int :
41+ minimums : dict [str , int ] = {}
42+
43+ for game_value in game_values :
44+ for color , value in game_value .items ():
45+ if color not in minimums :
46+ minimums [color ] = value
47+ else :
48+ minimums [color ] = max (value , minimums [color ])
49+ return math .prod (minimums .values ())
50+
51+
3752def find_possibilities (lines : list [str ]) -> int :
3853 # only 12 red cubes, 13 green cubes, and 14 blue cubes
3954 constraints = {
@@ -53,6 +68,16 @@ def find_possibilities(lines: list[str]) -> int:
5368 return sum_of_ids
5469
5570
71+ def find_minimum_possible (lines : list [str ]) -> int :
72+ total : int = 0
73+ for game_number , line in enumerate (lines , start = 1 ):
74+ game_values = parse_game (line )
75+
76+ total += check_minimum (game_values )
77+
78+ return total
79+
80+
5681@register_solution (2023 , 2 , 1 )
5782def part_one (input_data : list [str ]):
5883 answer = find_possibilities (input_data )
@@ -65,7 +90,7 @@ def part_one(input_data: list[str]):
6590
6691@register_solution (2023 , 2 , 2 )
6792def part_two (input_data : list [str ]):
68- answer = ...
93+ answer = find_minimum_possible ( input_data )
6994
7095 if not answer :
7196 raise SolutionNotFoundError (2023 , 2 , 2 )
0 commit comments