Created
December 4, 2023 09:24
-
-
Save vadim2404/569a1a3a79ba272d079dd5a91331672b to your computer and use it in GitHub Desktop.
day4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from typing import Set | |
def str_to_set(numbers: str) -> Set[int]: | |
return set(int(x) for x in numbers.strip().split(" ") if x) | |
ans = 0 | |
with open("input.txt") as f: | |
for line in f: | |
_, parts = line.split(":") | |
winning_numbers, player_numbers = parts.split("|") | |
winning_numbers = str_to_set(winning_numbers) | |
player_numbers = str_to_set(player_numbers) | |
common = len(winning_numbers & player_numbers) | |
if common > 0: | |
ans += 1 << (common - 1) | |
print(ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
from collections import defaultdict | |
from typing import Set | |
CARD_RE = re.compile(r"^Card\s+(\d+)") | |
def str_to_set(numbers: str) -> Set[int]: | |
return set(int(x) for x in numbers.strip().split(" ") if x) | |
cards = defaultdict(int) | |
max_card_id = 0 | |
with open("input.txt") as f: | |
for line in f: | |
card, parts = line.split(":") | |
card_id = int(CARD_RE.match(card).group(1)) | |
max_card_id = max(max_card_id, card_id) | |
cards[card_id] += 1 | |
winning_numbers, player_numbers = parts.split("|") | |
winning_numbers = str_to_set(winning_numbers) | |
player_numbers = str_to_set(player_numbers) | |
common = len(winning_numbers & player_numbers) | |
while common > 0: | |
cards[card_id + common] += cards[card_id] | |
common -= 1 | |
print(sum(value for key, value in cards.items() if key <= max_card_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment