advent/2024/3/two.py

27 lines
537 B
Python

import re
input = open("input").read()
regex = re.compile('mul\([0-9]{1,},[0-9]{1,}\)|don\'t\(\)|do\(\)')
results = regex.findall(input)
mul_enabled = True
total = 0
for i in results:
print(i)
if i == "do()":
mul_enabled = True
continue
elif i == "don't()":
mul_enabled = False
continue
if not mul_enabled:
continue
splitted = i.replace('(', ',').replace(')', ',').split(',')
left = int(splitted[1])
right = int(splitted[2])
total += left * right
print(total)