diff --git a/day02/__init__.py b/day02/__init__.py index e69de29..cea3d53 100644 --- a/day02/__init__.py +++ b/day02/__init__.py @@ -0,0 +1 @@ +"""day02 implementation.""" diff --git a/day02/day2.py b/day02/day2.py index ac71f83..dcfa584 100644 --- a/day02/day2.py +++ b/day02/day2.py @@ -1,3 +1,4 @@ +"""day2 solution.""" from enum import StrEnum INPUT = "day02/input.txt" @@ -5,27 +6,31 @@ class Color(StrEnum): + """Color enum.""" + RED = "red" GREEN = "green" BLUE = "blue" class Draw: + """Class representing a draw from the bag.""" + red: int = 0 green: int = 0 blue: int = 0 def __init__(self, string: str): - """string: `1 blue, 4 green, 5 red`""" + """string: ``1 blue, 4 green, 5 red``.""" self.parse_colors_count(string) def parse_color_count(self, string: str) -> tuple[int, Color]: - """string: `1 blue`""" + """string: ``1 blue``.""" num, color = string.split(" ") return int(num), Color(color) def parse_colors_count(self, string: str) -> None: - """string: `1 blue, 4 green, 5 red`""" + """string: ``1 blue, 4 green, 5 red``.""" for color_count in string.split(","): color_count = color_count.strip() num, color = self.parse_color_count(color_count) @@ -38,12 +43,19 @@ def parse_colors_count(self, string: str) -> None: class Game: + """Game class, showing multiple draws.""" + id: int red: int = 0 green: int = 0 blue: int = 0 def __init__(self, string: str): + """Create a game from an input string. + + Args: + string (str): a game string + """ game_id_str, draw_str = string.split(":") self.id = int(game_id_str.replace("Game ", "")) self.draws = self.parse_draws(draw_str) @@ -52,7 +64,7 @@ def __init__(self, string: str): self.blue = max(draw.blue for draw in self.draws) def parse_draws(self, string: str) -> list[Draw]: - """string: `1 blue; 4 green, 5 blue; 11 red, 3 blue`""" + """string: ``1 blue; 4 green, 5 blue; 11 red, 3 blue``.""" result = [] for draw_str in string.split(";"): draw_str = draw_str.strip() @@ -61,18 +73,28 @@ def parse_draws(self, string: str) -> list[Draw]: return result def __str__(self) -> str: + """Return summary of game id and minimal rgb.""" return f"Game {self.id}: {self.red},{self.green},{self.blue}" def power_level(self) -> int: + """Returns r*g*b.""" return self.red * self.green * self.blue def game_filter(game: Game) -> bool: - """Returns true if the game satisfies the constraints of Question 1""" + """Returns true if the game satisfies the constraints of Question 1.""" return game.red <= 12 and game.green <= 13 and game.blue <= 14 def get_games(input_file: str) -> list[Game]: + """Gets the games from the input file. + + Args: + input_file (str): input file name + + Returns: + list[Game]: list of ``Game``s + """ with open(input_file, "r", encoding="utf8") as file: games: list[Game] = [] for line in file: @@ -82,18 +104,18 @@ def get_games(input_file: str) -> list[Game]: def part1(games: list[Game]) -> int: - """Solves part 1""" + """Solves part 1.""" filtered_games = filter(game_filter, games) return sum(game.id for game in filtered_games) def part2(games: list[Game]) -> int: - """Solves part2""" + """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""" + """Parses data into data structures, then prints out answer to q1 and q2.""" games = get_games(INPUT) # Q1: diff --git a/day02/tests/__init__.py b/day02/tests/__init__.py index e69de29..8edb4c5 100644 --- a/day02/tests/__init__.py +++ b/day02/tests/__init__.py @@ -0,0 +1 @@ +"""tests for day02.""" diff --git a/day02/tests/test_day2.py b/day02/tests/test_day2.py index e6725b3..ac8de3e 100644 --- a/day02/tests/test_day2.py +++ b/day02/tests/test_day2.py @@ -1,16 +1,19 @@ +"""tests for day02.""" from day02.day2 import INPUT_SMALL, Draw, Game, game_filter, get_games, part1, part2 def get_game1() -> Game: + """Returns a sample game.""" return Game("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green") def get_game2() -> Game: + """Returns another sample game.""" return Game("Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue") def test_draw() -> None: - """Test draws""" + """Test draws.""" draw1: Draw = Draw("3 blue, 4 red") draw2: Draw = Draw("4 blue, 3 green") draw3: Draw = Draw("4 blue, 3 green, 1 red") @@ -21,7 +24,7 @@ def test_draw() -> None: def test_game() -> None: - """Test games""" + """Test games.""" game1: Game = get_game1() game2: Game = get_game2() @@ -40,7 +43,7 @@ def test_game() -> None: def test_part1() -> None: - """Tests get_games, game_filter, part1""" + """Tests get_games, game_filter, part1.""" games: list[Game] = get_games(INPUT_SMALL) # test game_filter @@ -52,7 +55,7 @@ def test_part1() -> None: def test_part2() -> None: - """Tests power""" + """Tests power.""" games: list[Game] = get_games(INPUT_SMALL) # test game_filter diff --git a/day03/__init__.py b/day03/__init__.py index e69de29..2c2e231 100644 --- a/day03/__init__.py +++ b/day03/__init__.py @@ -0,0 +1 @@ +"""day03 solution.""" diff --git a/day03/lib/__init__.py b/day03/lib/__init__.py index e69de29..5950102 100644 --- a/day03/lib/__init__.py +++ b/day03/lib/__init__.py @@ -0,0 +1 @@ +"""day03 classes.""" diff --git a/day03/tests/__init__.py b/day03/tests/__init__.py index e69de29..2c2e231 100644 --- a/day03/tests/__init__.py +++ b/day03/tests/__init__.py @@ -0,0 +1 @@ +"""day03 solution."""