Advertisement
hhoppe

Advent of code 2024 day 7 jit 340 ms

Dec 7th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. @numba.njit
  2. def day7_can_eval(desired, current, lst, part2):
  3.  
  4.   def count_digits(number):
  5.     return 1 if number == 0 else math.floor(math.log10(abs(number))) + 1
  6.  
  7.   if current > desired:
  8.     return False
  9.   if not lst:
  10.     return current == desired
  11.   part2_candidate = current * 10 ** count_digits(lst[0]) + lst[0]
  12.   return (
  13.       day7_can_eval(desired, current * lst[0], lst[1:], part2)
  14.       or day7_can_eval(desired, current + lst[0], lst[1:], part2)
  15.       or (part2 and day7_can_eval(desired, part2_candidate, lst[1:], part2))
  16.   )
  17.  
  18.  
  19. def day7(s, part2=False):
  20.   total = 0
  21.   for line in s.splitlines():
  22.     desired, *values = map(int, re.split(':? ', line))
  23.     if day7_can_eval(desired, values[0], values[1:], part2):
  24.       total += desired
  25.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement