|
1 | 1 | with open("input", "r") as input_file:
|
2 | 2 | input_lines: [str] = [a.strip() for a in input_file.read().strip().split("\n")]
|
3 | 3 |
|
| 4 | +# Parse the input into something like this: [card_id, (winning_numbers, owned_numbers)] |
4 | 5 | card_map: dict[int, (list[int], list[int])] = {}
|
5 | 6 | for input_line in input_lines:
|
6 | 7 | id_part, card = input_line.split(": ")
|
|
12 | 13 |
|
13 | 14 | card_map[card_id] = (winners, owneds)
|
14 | 15 |
|
| 16 | +# For each card type, pre-calculate which other card types it will win |
15 | 17 | win_map: dict[int, list[int]] = {}
|
16 | 18 | for card_id in card_map.keys():
|
17 | 19 | card: ([int], [int]) = card_map[card_id]
|
18 | 20 |
|
| 21 | + # Count how many wins we have on this card in total |
19 | 22 | winnerc: int = 0
|
20 | 23 | for winning in card[0]:
|
21 | 24 | if winning in card[1]:
|
22 | 25 | winnerc += 1
|
23 | 26 |
|
| 27 | + # Count through the list to see what exact cards are won |
24 | 28 | won_clones: [int] = []
|
25 | 29 | for i in range(winnerc):
|
26 | 30 | won_clones.append(card_id + i + 1)
|
27 | 31 | win_map[card_id] = won_clones
|
28 | 32 |
|
| 33 | +# We start with one of each card |
29 | 34 | owned_cards: dict[int, int] = dict([(i, 1) for i in card_map.keys()])
|
| 35 | +# Iterate through all card types to calculate how many copies we own |
30 | 36 | for card_id in card_map.keys():
|
31 | 37 | for won_clone_id in win_map[card_id]:
|
32 | 38 | owned_cards[won_clone_id] += owned_cards[card_id]
|
33 | 39 |
|
| 40 | +# The solution is the total amount of cards |
34 | 41 | solution: int = sum(owned_cards.values())
|
35 | 42 | print(f"SOLVE: {solution}")
|
0 commit comments