My Advent of Code solutions in Python.
kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code
python
1import fileinput
2from collections import Counter
3
4
5part_1 = 0
6part_2_cards = Counter()
7
8for line in fileinput.input():
9 card, rest = line.split(': ')
10 card_id = int(card[5:])
11
12 winning, your = rest.split(' | ')
13 winning = set(int(n) for n in winning.split())
14 your = [int(n) for n in your.split()]
15
16 # Add the "original" scratchcard as one of the copies.
17 part_2_cards[card_id] += 1
18
19 matches = 0
20 for n in your:
21 if n in winning:
22 matches += 1
23
24 if matches > 0:
25 part_1 += 2 ** (matches - 1)
26
27 for copy_id in range(card_id + 1, card_id + matches + 1):
28 part_2_cards[copy_id] += part_2_cards[card_id]
29
30print("Part 1:", part_1)
31print("Part 2:", sum(part_2_cards.values()))
32