-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__year__ = 2023 | ||
__day__ = 4 | ||
__title__ = "Scratchcards" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 | ||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 | ||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 | ||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 | ||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 | ||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from pydantic.dataclasses import dataclass | ||
|
||
from aoc.lib.solution import SolutionBase | ||
|
||
import logging | ||
|
||
|
||
__all__ = ["Solution"] | ||
__log__ = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class Card: | ||
id: int | ||
winning: set[int] | ||
numbers: set[int] | ||
|
||
|
||
class Solution(SolutionBase): | ||
@staticmethod | ||
def prepare(data): | ||
return tuple( | ||
Card( | ||
id=int(card[5:]), | ||
winning={int(number) for number in winning.split()}, | ||
numbers={int(number) for number in numbers.split()}, | ||
) | ||
for card, winning, numbers in ( | ||
line.replace("|", ":").split(":") for line in data | ||
) | ||
) | ||
|
||
@staticmethod | ||
def part_01(data): | ||
return sum(int(2 ** (len(card.winning & card.numbers) - 1)) for card in data) | ||
|
||
@staticmethod | ||
def part_02(data): | ||
copies = [1] * len(data[0].winning) | ||
result = 0 | ||
for card in data: | ||
_count = copies.pop(0) | ||
result += _count | ||
copies.append(1) | ||
for i in range(len(card.winning & card.numbers)): | ||
copies[i] += _count | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
import aoc.lib.main | ||
|
||
aoc.lib.main.main(Solution) |