diff --git a/README.md b/README.md index 1c14a8b..c75c257 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ | Day | Name | Part 1 | Part 2 | | :-: | :------------------ | :----: | :----: | | 1 | | ⭐ | ⭐ | -| 2 | | ⭐ | | +| 2 | | ⭐ | ⭐ | | 3 | | | | | 4 | | | | | 5 | | | | diff --git a/src/modules/__tests__/test_day_2.py b/src/modules/__tests__/test_day_2.py index 10b2bb4..326b7a5 100644 --- a/src/modules/__tests__/test_day_2.py +++ b/src/modules/__tests__/test_day_2.py @@ -5,3 +5,8 @@ def test_cube_game(): path = r"./src/assets/day_2_test.txt" game = GameRecord(path) assert game.sum_valid() == 8 + +def test_cube_game_power(): + path = r"./src/assets/day_2_test.txt" + game = GameRecord(path) + assert game.sum_power() == 2286 diff --git a/src/modules/day_2.py b/src/modules/day_2.py index 436cd35..44b0eeb 100644 --- a/src/modules/day_2.py +++ b/src/modules/day_2.py @@ -1,11 +1,11 @@ -from dataclasses import dataclass import re +from dataclasses import dataclass def run(): game = GameRecord(r"./src/assets/day_2.txt") - total_valid = game.sum_valid() - print(f"Total games valid: {total_valid}") + print(f"Total games valid: {game.sum_valid()}") + print(f"Total games power: {game.sum_power()}") @dataclass @@ -43,6 +43,25 @@ class GameRecord: return sum + def sum_power(self): + sum = 0 + for game in self.games: + red = 0 + green = 0 + blue = 0 + for play in game.plays: + for item in play: + match item[1]: + case "blue": + blue = max(blue,int(item[0])) + case "red": + red = max(red, int(item[0])) + case "green": + green = max(green,int(item[0])) + + sum += red * green * blue + + return sum def read_games(self) -> list[Game]: with open(self.path) as file: