From 92bd85e1dd8a388cc28b42c5ae2417b7dc28726f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Wed, 11 Dec 2024 07:13:28 +0100 Subject: [PATCH] 2024 day 11. --- src/holt59/aoc/2024/day11.py | 24 ++++++++++++++++++++- src/holt59/aoc/inputs/holt59/2024/day11.txt | 1 + src/holt59/aoc/inputs/tests/2024/day11.txt | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/holt59/aoc/2024/day11.py b/src/holt59/aoc/2024/day11.py index 07e201e..fea35bf 100644 --- a/src/holt59/aoc/2024/day11.py +++ b/src/holt59/aoc/2024/day11.py @@ -1,7 +1,29 @@ +from functools import cache from typing import Any, Iterator from ..base import BaseSolver +@cache +def blink_one_stone(stone: int, round: int) -> int: + if round == 0: + return 1 + + if stone == 0: + return blink_one_stone(1, round - 1) + + if len((stone_s := str(stone))) % 2 == 0: + n = len(stone_s) + return blink_one_stone(int(stone_s[: n // 2]), round - 1) + blink_one_stone( + int(stone_s[n // 2 :]), round - 1 + ) + + return blink_one_stone(stone * 2024, round - 1) + + class Solver(BaseSolver): - def solve(self, input: str) -> Iterator[Any]: ... + def solve(self, input: str) -> Iterator[Any]: + stones = list(map(int, input.split())) + + yield sum(blink_one_stone(stone, 25) for stone in stones) + yield sum(blink_one_stone(stone, 75) for stone in stones) diff --git a/src/holt59/aoc/inputs/holt59/2024/day11.txt b/src/holt59/aoc/inputs/holt59/2024/day11.txt index e69de29..4c7941d 100644 --- a/src/holt59/aoc/inputs/holt59/2024/day11.txt +++ b/src/holt59/aoc/inputs/holt59/2024/day11.txt @@ -0,0 +1 @@ +2 77706 5847 9258441 0 741 883933 12 diff --git a/src/holt59/aoc/inputs/tests/2024/day11.txt b/src/holt59/aoc/inputs/tests/2024/day11.txt index e69de29..9b26c84 100644 --- a/src/holt59/aoc/inputs/tests/2024/day11.txt +++ b/src/holt59/aoc/inputs/tests/2024/day11.txt @@ -0,0 +1 @@ +125 17