Skip to content

Commit

Permalink
docs: day02
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 29, 2023
1 parent 5538e35 commit d7e52fe
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions day02/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day02 implementation."""
38 changes: 30 additions & 8 deletions day02/day2.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
"""day2 solution."""
from enum import StrEnum

INPUT = "day02/input.txt"
INPUT_SMALL = "day02/input-small.txt"


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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions day02/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""tests for day02."""
11 changes: 7 additions & 4 deletions day02/tests/test_day2.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -21,7 +24,7 @@ def test_draw() -> None:


def test_game() -> None:
"""Test games"""
"""Test games."""
game1: Game = get_game1()
game2: Game = get_game2()

Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions day03/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day03 solution."""
1 change: 1 addition & 0 deletions day03/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day03 classes."""
1 change: 1 addition & 0 deletions day03/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day03 solution."""

0 comments on commit d7e52fe

Please sign in to comment.