|
| 1 | +def get_set_minimum(set: [dict[str, int]]) -> dict[str, int]: |
| 2 | + required: dict[str, int] = {} |
| 3 | + for color in set.keys(): |
| 4 | + required[color] = max(required.get(color, 0), set[color]) |
| 5 | + return required |
| 6 | + |
| 7 | + |
| 8 | +def get_game_minimum(game: list[dict[str, int]]) -> dict[str, int]: |
| 9 | + required: dict[str, int] = {} |
| 10 | + for set in game: |
| 11 | + set_required = get_set_minimum(set) |
| 12 | + for color in set_required.keys(): |
| 13 | + required[color] = max(required.get(color, 0), set_required[color]) |
| 14 | + return required |
| 15 | + |
| 16 | + |
| 17 | +with open("./input", "r") as input_file: |
| 18 | + input_lines: [str] = [a.strip() for a in input_file.read().strip().split("\n")] |
| 19 | + |
| 20 | +game_map: dict[int, list[dict[str, int]]] = {} |
| 21 | +for input_line in input_lines: |
| 22 | + line_parts: [str] = input_line.split(": ") |
| 23 | + game_id: int = int(line_parts[0].split(" ")[1]) |
| 24 | + text_game_sets: [[str]] = [part.split(", ") for part in line_parts[1].split("; ")] |
| 25 | + |
| 26 | + game_sets: list[dict[str, int]] = [] |
| 27 | + for text_game_set in text_game_sets: |
| 28 | + game_set: dict[str, int] = {} |
| 29 | + for set_element in text_game_set: |
| 30 | + parts: [str] = set_element.split(" ") |
| 31 | + amount: int = int(parts[0]) |
| 32 | + color: str = parts[1] |
| 33 | + game_set[color] = amount |
| 34 | + game_sets.append(game_set) |
| 35 | + game_map[game_id] = game_sets |
| 36 | + |
| 37 | +solution: int = 0 |
| 38 | +for game_id in game_map.keys(): |
| 39 | + game: list[dict[str, int]] = game_map[game_id] |
| 40 | + game_required: dict[str, int] = get_game_minimum(game) |
| 41 | + |
| 42 | + game_power: int = 1 |
| 43 | + for color in game_required.keys(): |
| 44 | + game_power *= game_required[color] |
| 45 | + solution += game_power |
| 46 | +print(f"SOLVE: {solution}") |
0 commit comments