Skip to content

Commit

Permalink
test: day02
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 23, 2023
1 parent 52706f6 commit f12e558
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
36 changes: 29 additions & 7 deletions day02/day2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from enum import StrEnum

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


class Color(StrEnum):
RED = "red"
Expand Down Expand Up @@ -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__":
Expand Down
5 changes: 5 additions & 0 deletions day02/input-small.txt
Original file line number Diff line number Diff line change
@@ -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
Empty file added day02/tests/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions day02/tests/test_day2.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f12e558

Please sign in to comment.