-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
34 lines (26 loc) · 1009 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
with open('input.txt', 'r') as file:
data = [int(x) for x in file.read().strip().split()]
def recursive_solve(stone, blinks, memo):
if (stone, blinks) in memo:
return memo[(stone, blinks)]
if blinks == 0:
result = 1
elif stone == 0:
result = recursive_solve(1, blinks - 1, memo)
elif len(str(stone)) % 2 == 0:
num_str = str(stone)
mid = len(num_str) // 2
result = recursive_solve(int(num_str[:mid]), blinks - 1, memo) + recursive_solve(int(num_str[mid:]),
blinks - 1, memo)
else:
result = recursive_solve(stone * 2024, blinks - 1, memo)
memo[(stone, blinks)] = result
return result
def solve(initial_stones, num_blinks):
memo = {}
total_stones = 0
for stone in initial_stones:
total_stones += recursive_solve(stone, num_blinks, memo)
return total_stones
print(solve(data, 25))
print(solve(data, 75))