Guest User

AoC day 3 part 1

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