From 17d8c24da40477faa02572458a88474acbf49f4f Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Wed, 26 Aug 2026 23:10:00 +0200 Subject: [PATCH] feat: day 4 part 2 done --- README.md | 2 +- src/modules/__tests__/test_day_4.py | 6 +++++- src/modules/day_4.py | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e41eeb..3be211b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ | 1 | | ⭐ | ⭐ | | 2 | | ⭐ | ⭐ | | 3 | | ⭐ | ⭐ | -| 4 | | ⭐ | | +| 4 | | ⭐ | ⭐ | | 5 | | | | | 6 | | | | | 7 | | | | diff --git a/src/modules/__tests__/test_day_4.py b/src/modules/__tests__/test_day_4.py index aa1be78..b29062c 100644 --- a/src/modules/__tests__/test_day_4.py +++ b/src/modules/__tests__/test_day_4.py @@ -1,5 +1,9 @@ -from src.modules.day_4 import count_scratchcard_points +from src.modules.day_4 import count_real_points, count_scratchcard_points def test_scratchcards(): assert count_scratchcard_points(r"src/assets/day_4_test.txt") == 13 + + +def test_scratchcards_real(): + assert count_real_points(r"src/assets/day_4_test.txt") == 30 diff --git a/src/modules/day_4.py b/src/modules/day_4.py index bd62000..900f664 100644 --- a/src/modules/day_4.py +++ b/src/modules/day_4.py @@ -3,6 +3,7 @@ import re def run(): print(f"Scratchcard points: {count_scratchcard_points(r'src/assets/day_4.txt')}") + print(f"Scratchcard real points: {count_real_points(r'src/assets/day_4.txt')}") def count_scratchcard_points(path: str): @@ -13,6 +14,31 @@ def count_scratchcard_points(path: str): return total +def count_real_points(path: str): + card_list = read_scratchcards(path) + card_index_list = [1] * card_list.__len__() + index = 0 + while index < card_list.__len__(): + card = card_list[index] + card_matchings = get_card_matchings(card) + + if card_matchings != 0: + for n_card_index in range(index + 1, index + card_matchings + 1): + card_index_list[n_card_index] += card_index_list[index] + + index += 1 + + return sum(card_index_list) + + +def get_card_matchings(card: tuple[list[int], list[int]]) -> int: + sum = 0 + for number in card[1]: + if number in card[0]: + sum += 1 + return sum + + def get_card_points(card: tuple[list[int], list[int]]) -> int: sum = 0 for number in card[1]: