Skip to content

Commit

Permalink
No need to keep the whole Set
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 4, 2023
1 parent b4a1ef9 commit e5e87c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Development occurs in language-specific directories:
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day1.kt)|[day1.py](py/aoc2023/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day2.kt)|[day2.py](py/aoc2023/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.kt)|[day3.py](py/aoc2023/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt)|||
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt)|[day4.py](py/aoc2023/day4.py)||
47 changes: 47 additions & 0 deletions py/aoc2023/day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Day 4: Scratchcards
"""

SAMPLE_INPUT = """
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
"""


def _parse(data):
for line in data.splitlines():
if not line:
continue
line = line[line.index(":") + 1 :]
left, right = line.split("|", maxsplit=1)
left = set(left.split())
right = set(right.split())
yield len(left & right)


def part1(data):
"""
>>> part1(SAMPLE_INPUT)
13
"""
return sum(1 << card >> 1 for card in _parse(data))


def part2(data):
"""
>>> part2(SAMPLE_INPUT)
30
"""
cards = list(_parse(data))
counts = [1 for _ in cards]
for i, card in enumerate(cards):
for j in range(card):
counts[i + j + 1] += counts[i]
return sum(counts)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ aoc2023 = "aoc2023.main:main"
day1 = "aoc2023.day1:parts"
day2 = "aoc2023.day2:parts"
day3 = "aoc2023.day3:parts"
day4 = "aoc2023.day4:parts"

[tool.black]
target_version = ["py312"]
Expand Down

0 comments on commit e5e87c2

Please sign in to comment.