-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_4.py
53 lines (42 loc) · 1.78 KB
/
day_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def score_card(wins: int):
if wins == 0:
return 0
else:
return 1 * pow(2, wins - 1)
# TODO: we could try to modify the code and adding caching here to speed up the scoring
def count_card_creation(card_winnings, number):
total = 0
created_cards = card_winnings.get(number, [])
total += len(created_cards)
for card in created_cards:
total += count_card_creation(card_winnings, card)
return total
def calculate_p1(cards: list[str]) -> int:
total = 0
for card in cards:
game = card.split(":")[1]
numbers, winning_numbers = game.split("|")
numbers = set(number for number in numbers.split(" ") if number.isdigit())
winning_numbers = set(number for number in winning_numbers.split(" ") if number.isdigit())
your_winning_numbers = numbers.intersection(winning_numbers)
score = score_card(len(your_winning_numbers))
total += score
return total
# TODO: smells like recursion
def calculate_p2(cards: list[str]) -> int:
total = 0
card_winnings = {}
for index, card in enumerate(cards):
game = card.split(":")[1]
numbers, winning_numbers = game.split("|")
numbers = set(number for number in numbers.split(" ") if number.isdigit())
winning_numbers = set(number for number in winning_numbers.split(" ") if number.isdigit())
your_winning_numbers = numbers.intersection(winning_numbers)
card_winnings[index] = [index + i + 1 for i in range(len(your_winning_numbers))]
for number in card_winnings.keys():
total += count_card_creation(card_winnings, number)
return total + len(cards)
with open("data/day_4.txt") as file:
cards = [line.strip() for line in file.readlines()]
print(calculate_p1(cards))
print(calculate_p2(cards))