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