Advertisement
hhoppe

Advent of code 2024 day 3

Dec 3rd, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. def day3(s, *, part2=False):
  2.   total = 0
  3.   enable = True
  4.   while s:
  5.     pattern_mul = r'^mul\(([0-9]{1,3}),([0-9]{1,3})\)'
  6.     if enable and (match := re.match(pattern_mul, s)):
  7.       total += int(match[1]) * int(match[2])
  8.     if s.startswith('do()'):
  9.       enable = True
  10.     if s.startswith("don't()") and part2:
  11.       enable = False
  12.     s = s[1:]
  13.  
  14.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement