50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import functools
|
||
|
|
||
|
stones = []
|
||
|
with open("day11") as f:
|
||
|
l = f.read().split(' ')
|
||
|
stones = list(map(int, l))
|
||
|
|
||
|
# 1
|
||
|
def part1():
|
||
|
for i in range(25):
|
||
|
s = 0
|
||
|
while s < len(stones):
|
||
|
match stones[s]:
|
||
|
case 0:
|
||
|
stones[s] = 1
|
||
|
s += 1
|
||
|
case num if len(str(num))%2==0:
|
||
|
l = len(str(num))
|
||
|
stones[s] = int(str(num)[:l//2])
|
||
|
stones.insert(s+1, int(str(num)[-l//2:]))
|
||
|
s += 2
|
||
|
case _:
|
||
|
stones[s] = stones[s] * 2024
|
||
|
s += 1
|
||
|
|
||
|
print(len(stones))
|
||
|
|
||
|
# 2
|
||
|
@functools.cache
|
||
|
def memo_me(stone, its):
|
||
|
if its == 0: return 1
|
||
|
|
||
|
if stone == 0:
|
||
|
return memo_me(1, its-1)
|
||
|
if len(str(stone))%2==0:
|
||
|
l = len(str(stone))
|
||
|
return memo_me(int(str(stone)[:l//2]), its-1) + memo_me(int(str(stone)[-l//2:]), its-1)
|
||
|
else:
|
||
|
return memo_me(stone*2024, its-1)
|
||
|
|
||
|
|
||
|
def part2():
|
||
|
num_stones = 0
|
||
|
for stone in stones:
|
||
|
num_stones += memo_me(stone, 75)
|
||
|
print(num_stones)
|
||
|
|
||
|
part2()
|