36 lines
No EOL
1.2 KiB
Python
36 lines
No EOL
1.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
lines = []
|
|
with open('./inputs/day04.txt') as f:
|
|
lines = f.readlines()
|
|
|
|
class scratch_card:
|
|
def __init__(self, card_input):
|
|
card = card_input.split(':')[1]
|
|
self.card_number = int(card_input.split(':')[0].split()[1])
|
|
numbers = card.split('|')
|
|
self.winning_numbers = list(map(lambda x: int(x.strip()), numbers[0].split()))
|
|
self.card_numbers = list(map(lambda x: int(x.strip()), numbers[1].split()))
|
|
self.winning_count = len(list(filter(lambda x: x in self.winning_numbers, self.card_numbers)))
|
|
self.card_score = 2 ** (self.winning_count - 1) if self.winning_count > 0 else 0
|
|
|
|
def __str__(self):
|
|
return f'({self.card_number}: {self.card_score}, {self.winning_count}, {self.winning_numbers}, {self.card_numbers})'
|
|
|
|
# part 1
|
|
cards = []
|
|
cards_by_number = {}
|
|
|
|
for line in lines:
|
|
cards.append(scratch_card(line))
|
|
cards_by_number[cards[-1].card_number] = cards[-1]
|
|
|
|
print(sum([card.card_score for card in cards]))
|
|
|
|
# part 2
|
|
for card in cards:
|
|
if card.winning_count > 0:
|
|
for i in range(1, card.winning_count + 1):
|
|
cards.append(cards_by_number[card.card_number + i])
|
|
|
|
print(len(cards)) |