|
| 1 | +class Game: |
| 2 | + |
| 3 | + def __init__(self, game_notation: str): |
| 4 | + [title, description] = game_notation.split(":") |
| 5 | + self.id = int(title.strip("Game ")) |
| 6 | + self.sets: [{"red": int, "green": int, "blue": int}] = [] |
| 7 | + self.is_possible = True |
| 8 | + set_descriptions = description.split(";") |
| 9 | + for set_description in set_descriptions: |
| 10 | + red = 0 |
| 11 | + green = 0 |
| 12 | + blue = 0 |
| 13 | + color_entries = set_description.split(",") |
| 14 | + for color_entry in color_entries: |
| 15 | + if "red" in color_entry: |
| 16 | + red = int(color_entry.strip(" red")) |
| 17 | + elif "green" in color_entry: |
| 18 | + green = int(color_entry.strip(" green")) |
| 19 | + elif "blue" in color_entry: |
| 20 | + blue = int(color_entry.strip(" blue")) |
| 21 | + self.sets.append({"red": red, "green": green, "blue": blue}) |
| 22 | + |
| 23 | + def get_power(self): |
| 24 | + min_red = 0 |
| 25 | + min_green = 0 |
| 26 | + min_blue = 0 |
| 27 | + |
| 28 | + for cube_set in self.sets: |
| 29 | + if cube_set["red"] > min_red: |
| 30 | + min_red = cube_set["red"] |
| 31 | + |
| 32 | + if cube_set["green"] > min_green: |
| 33 | + min_green = cube_set["green"] |
| 34 | + |
| 35 | + if cube_set["blue"] > min_blue: |
| 36 | + min_blue = cube_set["blue"] |
| 37 | + |
| 38 | + return min_red * min_green * min_blue |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + red_cubes = 12 |
| 43 | + green_cubes = 13 |
| 44 | + blue_cubes = 14 |
| 45 | + |
| 46 | + with open("day2_input.txt", "r") as input_file: |
| 47 | + input_string = input_file.read() |
| 48 | + |
| 49 | + input_lines = input_string.splitlines() |
| 50 | + game_powers_sum = 0 |
| 51 | + |
| 52 | + for idx, input_line in enumerate(input_lines): |
| 53 | + game_powers_sum += Game(input_line).get_power() |
| 54 | + |
| 55 | + print("Sum of game powers:", game_powers_sum) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + main() |
0 commit comments