Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day7(s, part2=False):
- total = 0
- for line in s.splitlines():
- desired, *values = map(int, re.split(':? ', line))
- def count_digits(number):
- return 1 if number == 0 else math.floor(math.log10(abs(number))) + 1
- def can_eval(current, lst):
- if not lst:
- return current == desired
- if current > desired: # Optional pruning speedup (from 2.9 s to 1.9 s).
- return False
- return (
- can_eval(current * lst[0], lst[1:])
- or can_eval(current + lst[0], lst[1:])
- or (part2 and can_eval(current * 10 ** count_digits(lst[0]) + lst[0], lst[1:]))
- )
- if can_eval(values[0], values[1:]):
- total += desired
- return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement