Skip to content

Commit

Permalink
day04: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 24, 2023
1 parent 1793a19 commit cdd6e7e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
Empty file added day03/lib/__init__.py
Empty file.
5 changes: 4 additions & 1 deletion day04/day4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Day 4 solution
"""

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


def split_numbers(string: str) -> set[int]:
"""
Expand Down Expand Up @@ -78,7 +81,7 @@ def grab_data(filename: str) -> list[Card]:


def main() -> None:
cards: list[Card] = grab_data("day04/input.txt")
cards: list[Card] = grab_data(INPUT)
# Q1
total_points = sum(card.get_points() for card in cards)
print(total_points)
Expand Down
6 changes: 6 additions & 0 deletions day04/input-small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
Empty file added day04/tests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions day04/tests/test_day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from day04.day4 import INPUT_SMALL, Card, Inventory, grab_data, split_numbers


def test_split_numbers() -> None:
assert split_numbers("6 7 8 9 10") == {6, 7, 8, 9, 10}
assert split_numbers("") == set()
assert split_numbers("6 6 6 6 6") == {6}


def test_grab_data() -> None:
cards: list[Card] = grab_data(INPUT_SMALL)
assert len(cards) == 6
assert cards[0].id == 1 and cards[-1].id == 6


def test_card() -> None:
card = Card("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53\n")
assert card.get_points() == 8
assert card.get_matches() == 4


def test_inventory() -> None:
cards: list[Card] = grab_data(INPUT_SMALL)
inventory: Inventory = Inventory(cards)
assert inventory.total_cards() == 30

0 comments on commit cdd6e7e

Please sign in to comment.