import sys from dataclasses import dataclass @dataclass(frozen=True) class Card: id: int numbers: list[int] values: list[int] lines = sys.stdin.read().splitlines() cards: list[Card] = [] for line in lines: id_part, e_part = line.split(":") numbers_s, values_s = e_part.split("|") cards.append( Card( id=int(id_part.split()[1]), numbers=[int(v.strip()) for v in numbers_s.strip().split()], values=[int(v.strip()) for v in values_s.strip().split()], ) ) winnings = [sum(1 for n in card.values if n in card.numbers) for card in cards] # part 1 answer_1 = sum(2 ** (winning - 1) for winning in winnings if winning > 0) print(f"answer 1 is {answer_1}") # part 2 card2cards = {i: list(range(i + 1, i + w + 1)) for i, w in enumerate(winnings)} card2values = {i: 0 for i in range(len(cards))} for i in range(len(cards)): card2values[i] += 1 for j in card2cards[i]: card2values[j] += card2values[i] print(f"answer 2 is {sum(card2values.values())}")