adventofcode2024/day11/sol.py

47 lines
1.1 KiB
Python

def solve(file):
nums = file.read().split()
for i in range(25):
if len(nums) < 100:
print(i, nums)
new = []
for n in nums:
if n == '0':
new.append('1')
elif len(n)%2 == 0:
new.append(n[:len(n)//2])
new.append(n[len(n)//2:].lstrip('0') or '0')
else:
new.append(str(int(n)*2024))
nums = new
print(len(nums))
def solve2(file):
nums = file.read().split()
print(sum(evolve(n, 25) for n in nums))
print(sum(evolve(n, 75) for n in nums))
book = {}
def evolve(n, t):
if t == 0:
return 1
elif (n,t) in book:
return book[n,t]
else:
if n == '0':
x = evolve('1', t-1)
elif len(n)%2 == 0:
x = evolve(n[:len(n)//2], t-1)
x += evolve(n[len(n)//2:].lstrip('0') or '0', t-1)
else:
x = evolve(str(int(n)*2024), t-1)
book[n,t] = x
return x
from io import StringIO
#solve(StringIO("0 1 10 99 999"))
solve2(StringIO("0 1 10 99 999"))
#solve(open("input"))
solve2(open("input"))