Skip to content

Commit 023807a

Browse files
committed
Implement 2024 day 11
1 parent 3e709cc commit 023807a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

2024/src/aoc/days/day11.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import functools
2+
from collections import defaultdict
3+
4+
from . import CombinedRunner
5+
6+
7+
@functools.cache
8+
def blink_num(num: int) -> tuple[int, ...]:
9+
if num == 0:
10+
return (1,)
11+
12+
num_str = str(num)
13+
num_len = len(num_str)
14+
15+
if num_len % 2 == 0:
16+
half = num_len // 2
17+
return (int(num_str[:half]), int(num_str[half:]))
18+
19+
return (num * 2024,)
20+
21+
22+
def step(nums: dict[int, int]) -> dict[int, int]:
23+
result = defaultdict(int)
24+
25+
for num, count in nums.items():
26+
for transformed in blink_num(num):
27+
result[transformed] += count
28+
29+
return result
30+
31+
32+
class DayRunner(CombinedRunner):
33+
@classmethod
34+
def run_both(cls, input: str) -> tuple[int, int]:
35+
nums = [int(val) for val in input.strip().split(" ")]
36+
37+
counts = defaultdict(int)
38+
39+
for num in nums:
40+
counts[num] += 1
41+
42+
for _ in range(25):
43+
counts = step(counts)
44+
45+
part1 = sum(counts.values())
46+
47+
for _ in range(50):
48+
counts = step(counts)
49+
50+
return part1, sum(counts.values())

2024/tests/test_day11.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from aoc.days.day11 import DayRunner
2+
3+
SAMPLE = "125 17"
4+
5+
6+
def test_sample_part1() -> None:
7+
assert DayRunner.part1(SAMPLE) == 55312

0 commit comments

Comments
 (0)