From cdd6e7ea7f9f028640078331260b6a81a0dd7887 Mon Sep 17 00:00:00 2001 From: alexo Date: Sun, 24 Dec 2023 12:19:38 +1100 Subject: [PATCH] day04: tests --- day03/lib/__init__.py | 0 day04/day4.py | 5 ++++- day04/input-small.txt | 6 ++++++ day04/tests/__init__.py | 0 day04/tests/test_day4.py | 25 +++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 day03/lib/__init__.py create mode 100644 day04/input-small.txt create mode 100644 day04/tests/__init__.py create mode 100644 day04/tests/test_day4.py diff --git a/day03/lib/__init__.py b/day03/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day04/day4.py b/day04/day4.py index 05ab355..33ae402 100644 --- a/day04/day4.py +++ b/day04/day4.py @@ -2,6 +2,9 @@ Day 4 solution """ +INPUT = "day04/input.txt" +INPUT_SMALL = "day04/input-small.txt" + def split_numbers(string: str) -> set[int]: """ @@ -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) diff --git a/day04/input-small.txt b/day04/input-small.txt new file mode 100644 index 0000000..9bdb874 --- /dev/null +++ b/day04/input-small.txt @@ -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 diff --git a/day04/tests/__init__.py b/day04/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day04/tests/test_day4.py b/day04/tests/test_day4.py new file mode 100644 index 0000000..40da0ea --- /dev/null +++ b/day04/tests/test_day4.py @@ -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