|
| 1 | +import math |
| 2 | +from dataclasses import dataclass |
| 3 | + |
| 4 | +from advent_of_code.util import format_solution, puzzle_input |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class ScratchCard: |
| 9 | + #: Card number |
| 10 | + number: int |
| 11 | + |
| 12 | + winning_numbers: set[int] |
| 13 | + |
| 14 | + your_numbers: set[int] |
| 15 | + |
| 16 | + def points_value(self): |
| 17 | + matches = len(self.winning_numbers & self.your_numbers) |
| 18 | + return int(math.pow(2, matches - 1)) if matches > 0 else 0 |
| 19 | + |
| 20 | + def card_winnings(self) -> list[int]: |
| 21 | + matches = len(self.winning_numbers & self.your_numbers) |
| 22 | + return [self.number + i + 1 for i in range(matches)] |
| 23 | + |
| 24 | + |
| 25 | +def parse_card(card: str) -> ScratchCard: |
| 26 | + card_info, nums_part = card.split(":") |
| 27 | + card_num = int(card_info.split("Card")[1].strip()) |
| 28 | + |
| 29 | + winning_nums_part, your_nums_part = nums_part.split("|") |
| 30 | + |
| 31 | + winning_nums = {int(n.strip()) for n in winning_nums_part.split(" ") if len(n) > 0} |
| 32 | + your_nums = {int(n.strip()) for n in your_nums_part.split(" ") if len(n) > 0} |
| 33 | + |
| 34 | + return ScratchCard( |
| 35 | + number=card_num, |
| 36 | + winning_numbers=winning_nums, |
| 37 | + your_numbers=your_nums, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def part_1(cards_input: list[str]) -> int: |
| 42 | + cards = [parse_card(line) for line in cards_input] |
| 43 | + |
| 44 | + return sum(card.points_value() for card in cards) |
| 45 | + |
| 46 | + |
| 47 | +def part_2(cards_input: list[str]) -> int: |
| 48 | + cards = sorted([parse_card(line) for line in cards_input], key=lambda c: c.number) |
| 49 | + |
| 50 | + card_counts = {card.number: 1 for card in cards} |
| 51 | + |
| 52 | + for card in cards: |
| 53 | + for new_card_num in card.card_winnings(): |
| 54 | + card_counts[new_card_num] += card_counts[card.number] |
| 55 | + |
| 56 | + return sum(card_counts.values()) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + puzzle = puzzle_input(2023, 4) |
| 61 | + |
| 62 | + print( |
| 63 | + format_solution( |
| 64 | + solver_p1=lambda: part_1(puzzle), |
| 65 | + solver_p2=lambda: part_2(puzzle), |
| 66 | + ) |
| 67 | + ) |
0 commit comments