Skip to content

Commit

Permalink
Solve day 4 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mjalkio committed Dec 25, 2023
1 parent 467ee41 commit f7b4a02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 18 additions & 1 deletion year_2023/day04/scratchcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
5 changes: 5 additions & 0 deletions year_2023/day04/test_scratchcards.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f7b4a02

Please sign in to comment.