Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import cache
- from sys import argv
- MAX_DEPTH = 75
- def parse(src):
- '''Convert the input to a list of integers.'''
- return [int(x) for x in src.split()]
- def count_digits(num):
- '''Count the digits of num in base 10'''
- total = 0
- while num > 0:
- total += 1
- num = num // 10
- return total
- @cache
- def expand(stone, depth=0):
- '''Traverse the 'timeline' depth-first and return the number of stones
- created by blinking at stone from depth to MAX_DEPTH blinks.'''
- while depth < MAX_DEPTH:
- if stone == 0:
- stone = 1
- depth += 1
- continue
- digits = count_digits(stone)
- if digits % 2 == 0:
- mask = 10 ** (digits // 2)
- left = stone // mask
- right = stone % mask
- return expand(left, depth + 1) + expand(right, depth + 1)
- stone *= 2024
- depth += 1
- return 1
- def main(stones):
- '''The total number of stones produced after 75 blinks'''
- return sum(expand(stone) for stone in stones)
- if __name__ == '__main__':
- # pass the path to the puzzle input as the first argument
- print(main(parse(open(argv[1]).read())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement