diff --git a/year_2023/day04/scratchcards.py b/year_2023/day04/scratchcards.py index f73f122..01033d8 100644 --- a/year_2023/day04/scratchcards.py +++ b/year_2023/day04/scratchcards.py @@ -23,8 +23,25 @@ def get_point_value(puzzle_input): return point_value +def get_num_scratchcards(puzzle_input): + card_inputs = puzzle_input.split("\n") + num_scratchcards = {i: 1 for i in range(1, len(card_inputs) + 1)} + for card_num in num_scratchcards: + winning_input, number_input = ( + card_inputs[card_num - 1].split(": ")[1].split(" | ") + ) + winning_numbers = _split_numbers(winning_input) + numbers_you_have = _split_numbers(number_input) + winners = [num for num in numbers_you_have if num in winning_numbers] + num_wins = len(winners) + for i in range(card_num + 1, card_num + num_wins + 1): + num_scratchcards[i] += num_scratchcards[card_num] + # print(num_scratchcards) + return sum(num_scratchcards.values()) + + if __name__ == "__main__": puzzle_input = read_puzzle_input() print(f"Part 1: {get_point_value(puzzle_input)}") - print(f"Part 2: {get_point_value(puzzle_input)}") + print(f"Part 2: {get_num_scratchcards(puzzle_input)}") diff --git a/year_2023/day04/test_scratchcards.py b/year_2023/day04/test_scratchcards.py index 1c67b4d..f9712b7 100644 --- a/year_2023/day04/test_scratchcards.py +++ b/year_2023/day04/test_scratchcards.py @@ -1,8 +1,13 @@ from util import read_puzzle_input from year_2023.day04.scratchcards import ( + get_num_scratchcards, get_point_value, ) def test_get_point_value(): assert get_point_value(read_puzzle_input("test_input.txt")) == 13 + + +def test_get_num_scratchcards(): + assert get_num_scratchcards(read_puzzle_input("test_input.txt")) == 30