Skip to content

Commit

Permalink
add year 2023 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
NimVek committed Dec 4, 2023
1 parent 1957774 commit 20d0f2e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions y2023/d04/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__year__ = 2023
__day__ = 4
__title__ = "Scratchcards"
6 changes: 6 additions & 0 deletions y2023/d04/cases/example.13.30.txt
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
53 changes: 53 additions & 0 deletions y2023/d04/solution.py
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)

0 comments on commit 20d0f2e

Please sign in to comment.