From cb2e955be75986a239fc44c6bccc6c72ff29715d Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sat, 14 Dec 2024 00:11:30 +0100 Subject: [PATCH] 2024 day 11 parts 2 --- README.md | 2 +- src/year2024/day11b.py | 26 ++++++++++++++++++++++++++ tests/src/year2024/test_day11b.py | 8 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/year2024/day11b.py create mode 100644 tests/src/year2024/test_day11b.py diff --git a/README.md b/README.md index 3e20a49..33f8d61 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★ - 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆ - 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆ -- 2024 - ★★★★★ ★★★★★ ☆ +- 2024 - ★★★★★ ★★★★★ ★ ## How to use diff --git a/src/year2024/day11b.py b/src/year2024/day11b.py new file mode 100644 index 0000000..368041e --- /dev/null +++ b/src/year2024/day11b.py @@ -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) diff --git a/tests/src/year2024/test_day11b.py b/tests/src/year2024/test_day11b.py new file mode 100644 index 0000000..192e095 --- /dev/null +++ b/tests/src/year2024/test_day11b.py @@ -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