All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Mikael CAPELLE <mikael.capelle@thalesaleniaspace.com> Co-authored-by: Mikaël Capelle <capelle.mikael@gmail.com> Reviewed-on: #3
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
from dataclasses import dataclass
|
|
from typing import Any, Iterator
|
|
|
|
from ..base import BaseSolver
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Card:
|
|
id: int
|
|
numbers: list[int]
|
|
values: list[int]
|
|
|
|
|
|
class Solver(BaseSolver):
|
|
def solve(self, input: str) -> Iterator[Any]:
|
|
lines = input.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
|
|
yield sum(2 ** (winning - 1) for winning in winnings if winning > 0)
|
|
|
|
# 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]
|
|
|
|
yield sum(card2values.values())
|