Skip to content

Commit

Permalink
2024 day 11 parts 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 13, 2024
1 parent 24934fb commit cb2e955
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆
- 2024 - ★★★★★ ★★★★★
- 2024 - ★★★★★ ★★★★★

## How to use

Expand Down
26 changes: 26 additions & 0 deletions src/year2024/day11b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""2024 - Day 11 Part 2: Plutonian Pebbles"""

from functools import cache


@cache
def evolve(stone: int, generation: int) -> int:
if generation == 0:
return 1

if stone == 0:
return evolve(1, generation - 1)
elif len(str(stone)) % 2 == 0:
middle = len(str(stone)) // 2

left = int(str(stone)[:middle])
right = int(str(stone)[middle:])

return evolve(left, generation - 1) + evolve(right, generation - 1)
else:
return evolve(stone * 2024, generation - 1)


def solve(task: str) -> int:
stones = [int(x) for x in task.split()]
return sum(evolve(stone, 75) for stone in stones)
8 changes: 8 additions & 0 deletions tests/src/year2024/test_day11b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""2024 - Day 11 Part 2: Plutonian Pebbles"""

from src.year2024.day11b import solve


def test_solve():
task = "125 17"
assert solve(task) == 65601038650482

0 comments on commit cb2e955

Please sign in to comment.