44 lines
964 B
Python
44 lines
964 B
Python
import functools
|
|
|
|
with open('input.text', 'r') as file:
|
|
data = file.read()
|
|
|
|
stones: dict[int, int] = {}
|
|
for n in data.split():
|
|
n = int(n)
|
|
if n not in stones:
|
|
stones[n] = 1
|
|
else:
|
|
stones[n] += 1
|
|
|
|
blinks = 75
|
|
|
|
@functools.cache
|
|
def process_stone(n: int) -> list[int]:
|
|
|
|
if n == 0:
|
|
return [1]
|
|
elif (digits := len(str(n))) % 2 == 0:
|
|
split: int = digits // 2
|
|
return [int(str(n)[:split]), int(str(n)[split:])]
|
|
else:
|
|
return [n * 2024]
|
|
|
|
def blink(stones: dict[int, int]) -> dict[int, int]:
|
|
counts: dict[int, int] = {}
|
|
|
|
for n, count in stones.items():
|
|
|
|
new_stones: list[int] = process_stone(n)
|
|
for new_stone in new_stones:
|
|
if new_stone not in counts:
|
|
counts[new_stone] = count
|
|
else:
|
|
counts[new_stone] += count
|
|
|
|
return counts
|
|
|
|
for i in range(blinks):
|
|
stones = blink(stones)
|
|
print(i + 1, sum(stones.values()))
|