Advertisement
hhoppe

Advent of code 2024 day 11

Dec 10th, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. def day11(s, *, part2=False):
  2.  
  3.   @functools.cache
  4.   def length(e: str, num_steps: int) -> int:
  5.     if num_steps == 0:
  6.       return 1
  7.     if e == '0':
  8.       return length('1', num_steps - 1)
  9.     if len(e) % 2 == 0:
  10.       e1, e2 = e[: len(e) // 2], (e[len(e) // 2 :].lstrip('0') or '0')
  11.       return length(e1, num_steps - 1) + length(e2, num_steps - 1)
  12.     return length(str(int(e) * 2024), num_steps - 1)
  13.  
  14.   return sum(length(e, (25, 75)[part2]) for e in s.split())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement