Guest User

AoC day 3 part 2

a guest
Dec 3rd, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | Source Code | 0 0
  1. data = open('input.txt').read().strip()
  2. do = True
  3. total = 0
  4. for i in range(len(data)):
  5.     chunk = data[i:]
  6.  
  7.     if chunk.startswith('do()'):
  8.         do = True
  9.  
  10.     elif chunk.startswith("don't()"):
  11.         do = False
  12.  
  13.     if not chunk.startswith('mul('):
  14.         continue
  15.  
  16.     subchunk = chunk[4:] # 4 = "mul(" length
  17.     try:
  18.  
  19.         pairs = subchunk.split(')', 1)[0].split(',')
  20.         a = int(pairs[0])
  21.         b = int(pairs[1])
  22.         if do:
  23.             total += a * b
  24.  
  25.     except:
  26.         continue
  27.  
  28. print(total)
Advertisement
Add Comment
Please, Sign In to add comment