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