47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import re
|
|
import functools
|
|
|
|
cards = []
|
|
|
|
with open("input.txt", "r") as f:
|
|
for line in f:
|
|
card_no, numbers = line.strip().split(": ")
|
|
winning, mine = numbers.strip().split(" | ")
|
|
winning = re.split("\\s+", winning.strip())
|
|
mine = re.split("\\s+", mine.strip())
|
|
winning = list(map(int, winning))
|
|
mine = list(map(int, mine))
|
|
|
|
cards.append((winning, mine))
|
|
|
|
results = []
|
|
|
|
for winning, mine in cards:
|
|
my_winning_numbers = []
|
|
for my_number in mine:
|
|
if my_number in winning:
|
|
my_winning_numbers.append(my_number)
|
|
if len(my_winning_numbers) == 0:
|
|
results.append(0)
|
|
else:
|
|
results.append(2 ** (len(my_winning_numbers) - 1))
|
|
|
|
part1 = functools.reduce(lambda a, b: a + b, results)
|
|
print(part1)
|
|
|
|
card_counts = {}
|
|
|
|
for i, card in enumerate(cards):
|
|
card_counts[i] = 1
|
|
|
|
for i, (winning, mine) in enumerate(cards):
|
|
this_card_count = card_counts[i]
|
|
winning_count = 0
|
|
for my_number in mine:
|
|
if my_number in winning:
|
|
winning_count += 1
|
|
for next in range(1, winning_count + 1):
|
|
card_counts[i + next] += this_card_count
|
|
|
|
part2 = functools.reduce(lambda a, b: a + b, card_counts.values())
|
|
print(part2)
|