Created
December 5, 2023 17:05
-
-
Save luqmansen/3793d09d23936c976965662ec2ae9b36 to your computer and use it in GitHub Desktop.
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
@dataclass | |
class Deck: | |
id: int | |
winning: Set[int] | |
mine: Set[int] | |
def count_point(self) -> int: | |
mine_winning = self.winning.intersection(self.mine) | |
num_of_match = len(mine_winning) | |
if num_of_match <= 1: | |
return num_of_match | |
else: | |
return 2**(num_of_match-1) | |
def parse_raw_cards(raw_cards: str) -> List[int]: | |
return [ | |
(int(c)) for c | |
in raw_cards.strip().split(' ') | |
if c != '' | |
] | |
def solve(): | |
decks = [] | |
for line in _input.splitlines(): | |
deck, card = line.split(':') | |
deck_id = deck.split(' ')[-1] | |
raw_winning, raw_mine = card.split('|') | |
decks.append( | |
Deck( | |
id=int(deck_id), | |
winning=set(parse_raw_cards(raw_winning)), | |
mine=set(parse_raw_cards(raw_mine)) | |
) | |
) | |
print(sum([deck.count_point() for deck in decks])) | |
if __name__ == '__main__': | |
solve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment