Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = open('input.txt').read().strip()
- total = 0
- for i in range(len(data)):
- chunk = data[i:]
- if not chunk.startswith('mul('):
- continue
- chunk = chunk[4:] # 4 = "mul(" length
- try:
- subchunk = chunk.split(')',1)[0]
- pair = subchunk.split(',')
- a = int(pair[0])
- b = int(pair[1])
- total += a * b
- except:
- continue
- print(total)
Advertisement
Add Comment
Please, Sign In to add comment