Skip to content

Commit 8834bcb

Browse files
committed
[py] 2023:04: Comment part2 solution
I felt like this was too Ãconvoluted to let it stand without comments or any debugging output
1 parent c387bed commit 8834bcb

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

2023/day04/part2.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
with open("input", "r") as input_file:
22
input_lines: [str] = [a.strip() for a in input_file.read().strip().split("\n")]
33

4+
# Parse the input into something like this: [card_id, (winning_numbers, owned_numbers)]
45
card_map: dict[int, (list[int], list[int])] = {}
56
for input_line in input_lines:
67
id_part, card = input_line.split(": ")
@@ -12,24 +13,30 @@
1213

1314
card_map[card_id] = (winners, owneds)
1415

16+
# For each card type, pre-calculate which other card types it will win
1517
win_map: dict[int, list[int]] = {}
1618
for card_id in card_map.keys():
1719
card: ([int], [int]) = card_map[card_id]
1820

21+
# Count how many wins we have on this card in total
1922
winnerc: int = 0
2023
for winning in card[0]:
2124
if winning in card[1]:
2225
winnerc += 1
2326

27+
# Count through the list to see what exact cards are won
2428
won_clones: [int] = []
2529
for i in range(winnerc):
2630
won_clones.append(card_id + i + 1)
2731
win_map[card_id] = won_clones
2832

33+
# We start with one of each card
2934
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
3036
for card_id in card_map.keys():
3137
for won_clone_id in win_map[card_id]:
3238
owned_cards[won_clone_id] += owned_cards[card_id]
3339

40+
# The solution is the total amount of cards
3441
solution: int = sum(owned_cards.values())
3542
print(f"SOLVE: {solution}")

0 commit comments

Comments
 (0)