Skip to content

Instantly share code, notes, and snippets.

@luqmansen
Created December 5, 2023 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqmansen/3793d09d23936c976965662ec2ae9b36 to your computer and use it in GitHub Desktop.
Save luqmansen/3793d09d23936c976965662ec2ae9b36 to your computer and use it in GitHub Desktop.
@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