summaryrefslogtreecommitdiff
path: root/04/main.py
blob: dfe63d9cab525974273d75ae081cc6a0ec182612 (plain)
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
#!/bin/python3
import sys
import re

card_count_map = {}
for line in sys.stdin:
  line = line.strip()
  card_id, sets = line.split(":")
  card_id = int(re.sub("[^0-9]", "", card_id))
  card_count_map[card_id] = card_count_map.get(card_id, 1)

  winning_numbers, holding_numbers = [set(x.split()) for x in sets.split("|")]

  matches = 0
  for num in holding_numbers:
    if num in winning_numbers:
      matches += 1

  print(f"card {card_id} has {matches} matches")
  for x in range(matches):
    index = card_id + x + 1
    card_count_map[index] = card_count_map.get(index, 1) + card_count_map[card_id]
    print(f"card_count_map[{index}] = {card_count_map[index]}")

print(f">> {sum(list(card_count_map.values()))}")