Skip to content

Commit 0cc2920

Browse files
committed
feat: day 11 complete
1 parent 53fcb4c commit 0cc2920

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

2024/inputs/day11.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1750884 193 866395 7 1158 31 35216 0

2024/inputs/tests/test_day11.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
125 17

2024/python/src/grice_py_aoc_2024/day11/main.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
from __future__ import annotations
22

33
import time
4+
from functools import cache
45
from pathlib import Path
56

67
DIR = Path(__file__).parent
78
FILE = DIR.parents[3] / "inputs" / f"{DIR.stem}.txt"
89

910

10-
def part1(contents: str):
11-
return "Not done yet!"
11+
@cache
12+
def blink(num: int, iters: int) -> int:
13+
# Borrowed some insight from folks in PyDis.
14+
# We don't need to store most of the real numbers, just the number of stones that
15+
# result from iterating on a particular stone N times.
16+
# Combined with caching results of stone X iterated N times,
17+
# we get a very quick response in both parts.
18+
if iters == 0:
19+
return 1
20+
if num == 0:
21+
return blink(1, iters - 1)
22+
if len(numstr := str(num)) % 2 == 0:
23+
half = len(numstr) // 2
24+
return blink(int(numstr[half:]), iters - 1) + blink(
25+
int(numstr[:half]),
26+
iters - 1,
27+
)
28+
return blink(num * 2024, iters - 1)
1229

1330

14-
def part2(contents: str):
15-
return "Not done yet!"
31+
def part1(contents: list[int]) -> int:
32+
return sum(blink(x, 25) for x in contents)
33+
34+
35+
def part2(contents: list[int]) -> int:
36+
return sum(blink(x, 75) for x in contents)
1637

1738

1839
def main():
19-
contents = FILE.read_text()
40+
contents = list(map(int, FILE.read_text().strip().split()))
2041

2142
_start1 = time.perf_counter()
2243
result1 = part1(contents)

2024/python/src/grice_py_aoc_2024/day11/test_day11.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
DIR = Path(__file__).parent
1010
TEST_FILE = DIR.parents[3] / "inputs/tests" / f"test_{DIR.stem}.txt"
11-
EXPECTED_PART_1 = "REPLACE ME!"
11+
EXPECTED_PART_1 = 55312
1212
EXPECTED_PART_2 = "REPLACE ME!"
1313

1414

1515
@pytest.mark.skip(reason=f"{DIR.stem} P1 incomplete")
1616
def test_part1():
17-
contents = TEST_FILE.read_text()
17+
contents = list(map(int, TEST_FILE.read_text().strip().split()))
1818
result = part1(contents)
1919
assert result == EXPECTED_PART_1
2020

2121

2222
@pytest.mark.xfail(reason=f"{DIR.stem} P1 incomplete")
2323
def test_part2():
24-
contents = TEST_FILE.read_text()
24+
contents = list(map(int, TEST_FILE.read_text().strip().split()))
2525
result = part2(contents)
2626
assert result == EXPECTED_PART_2

0 commit comments

Comments
 (0)