From 34c57b32b0efcc36bb9a1760a5ebf64b2cbde928 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Wed, 11 Dec 2024 01:54:18 -0500 Subject: [PATCH] Day 11: Plutonian Pebbles --- README.md | 2 +- py/aoc2024/day11.py | 42 ++++++++++++++++++++++++++++++++++++++++++ py/pyproject.toml | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 py/aoc2024/day11.py diff --git a/README.md b/README.md index 4ef7297..73a7d6a 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ Development occurs in language-specific directories: |[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt)|[day8.py](py/aoc2024/day8.py)|[day8.rs](rs/src/day8.rs)| |[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.kt)|[day9.py](py/aoc2024/day9.py)|[day9.rs](rs/src/day9.rs)| |[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)|[day10.rs](rs/src/day10.rs)| -|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)||| +|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)|| diff --git a/py/aoc2024/day11.py b/py/aoc2024/day11.py new file mode 100644 index 0000000..1e31a94 --- /dev/null +++ b/py/aoc2024/day11.py @@ -0,0 +1,42 @@ +""" +Day 11: Plutonian Pebbles +""" + +from collections import Counter + + +def part1(data: str) -> int: + return _solve(data, 25) + + +def part2(data: str) -> int: + return _solve(data, 75) + + +def _solve(data: str, n: int) -> int: + """ + >>> _solve("0 1 10 99 999", 1) + 7 + >>> _solve("125 17", 6) + 22 + >>> _solve("125 17", 25) + 55312 + """ + counts = Counter(map(int, data.split())) + for i in range(n): + next = Counter() + for num, count in counts.items(): + if num == 0: + next[1] += count + else: + string = str(num) + if len(string) % 2 == 0: + next[int(string[: len(string) // 2])] += count + next[int(string[len(string) // 2 :])] += count + else: + next[2024 * num] += count + counts = next + return counts.total() + + +parts = (part1, part2) diff --git a/py/pyproject.toml b/py/pyproject.toml index c67e318..7bfffa4 100644 --- a/py/pyproject.toml +++ b/py/pyproject.toml @@ -34,6 +34,7 @@ day7 = "aoc2024.day7:parts" day8 = "aoc2024.day8:parts" day9 = "aoc2024.day9:parts" day10 = "aoc2024.day10:parts" +day11 = "aoc2024.day11:parts" [build-system] requires = ["poetry-core"]