diff --git a/day02/day2.py b/day02/day2.py index 7c6d922..63a5f8c 100644 --- a/day02/day2.py +++ b/day02/day2.py @@ -1,5 +1,8 @@ from enum import StrEnum +INPUT = "day02/input.txt" +INPUT_SMALL = "day02/input-small.txt" + class Color(StrEnum): RED = "red" @@ -63,25 +66,44 @@ def __str__(self) -> str: def power_level(self) -> int: return self.red * self.green * self.blue + def __repr__(self) -> str: + return str(self) + def game_filter(game: Game) -> bool: """Returns true if the game satisfies the constraints of Question 1""" return game.red <= 12 and game.green <= 13 and game.blue <= 14 -def main() -> None: - """Parses data into data structures, then prints out answer to q1 and q2""" - with open("day02/input.txt", "r", encoding="utf8") as file: - games = [] +def get_games(input_file: str) -> list[Game]: + with open(input_file, "r", encoding="utf8") as file: + games: list[Game] = [] for line in file: game = Game(line) games.append(game) + return games - # Q1: + +def part1(games: list[Game]) -> int: + """Solves part 1""" filtered_games = filter(game_filter, games) - print(sum(game.id for game in filtered_games)) + return sum(game.id for game in filtered_games) + + +def part2(games: list[Game]) -> int: + """Solves part2""" + return sum(game.power_level() for game in games) + + +def main() -> None: + """Parses data into data structures, then prints out answer to q1 and q2""" + games = get_games(INPUT) + + # Q1: + print(part1(games)) + # Q2: - print(sum(game.power_level() for game in games)) + print(part2(games)) if __name__ == "__main__": diff --git a/day02/input-small.txt b/day02/input-small.txt new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/day02/input-small.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green diff --git a/day02/tests/__init__.py b/day02/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day02/tests/test_day2.py b/day02/tests/test_day2.py new file mode 100644 index 0000000..f65babc --- /dev/null +++ b/day02/tests/test_day2.py @@ -0,0 +1,49 @@ +from day02.day2 import INPUT_SMALL, Game, game_filter, get_games, part1, part2 + + +def get_game1() -> Game: + return Game("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green") + + +def get_game2() -> Game: + return Game("Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue") + + +def test_game() -> None: + # constructor: + game1: Game = get_game1() + game2: Game = get_game2() + + assert game1.id == 1 + assert game1.red == 4 + assert game1.blue == 6 + assert game1.green == 2 + + assert game2.id == 2 + assert game2.red == 1 + assert game2.green == 3 + assert game2.blue == 4 + + +def test_part1() -> None: + """Tests get_games, game_filter, part1""" + games: list[Game] = get_games(INPUT_SMALL) + + # test game_filter + expected: list[bool] = [True, True, False, False, True] + for index, game in enumerate(games): + assert game_filter(game) == expected[index] + # test part1() + assert part1(games) == 8 + + +def test_part2() -> None: + """Tests power""" + games: list[Game] = get_games(INPUT_SMALL) + + # test game_filter + expected = [48, 12, 1560, 630, 36] + for index, game in enumerate(games): + assert game.power_level() == expected[index] + # test part1() + assert part2(games) == 2286