memoize = {} class Globals: memoize_used = 0 def count_digits(number: int) -> int: if number == 0: return 1 count = 0 while number != 0: number = number // 10 count += 1 return count def count_after_blinks(stone: int, blink_remaining: int): if blink_remaining == 0: return 1 elif (stone, blink_remaining) in memoize: Globals.memoize_used += 1 return memoize[(stone, blink_remaining)] else: if stone == 0: result = count_after_blinks(1, blink_remaining - 1) else: digits = count_digits(stone) if digits % 2 == 0: divisor = 10 ** (digits // 2) result = count_after_blinks(stone % divisor, blink_remaining - 1) + count_after_blinks(stone // divisor, blink_remaining - 1) else: result = count_after_blinks(stone * 2024, blink_remaining - 1) memoize[(stone, blink_remaining)] = result return result data = [] with open("input") as f: data.extend((int(elem) for elem in f.read().strip().split())) count = 0 for elem in data: count += count_after_blinks(elem, 75) print(f"elem {elem} done") print(Globals.memoize_used) print(count)