23 lines
716 B
Python
23 lines
716 B
Python
import re
|
|
|
|
example = """xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"""
|
|
input = open('input.txt').read()
|
|
|
|
def process_instructions(text):
|
|
mul_enabled = True
|
|
total_sum = 0
|
|
|
|
pattern = re.compile(r"(mul\((\d+),(\d+)\))|do\(\)|don't\(\)")
|
|
for match in pattern.finditer(text):
|
|
if match.group(1):
|
|
if mul_enabled:
|
|
num1 = int(match.group(2))
|
|
num2 = int(match.group(3))
|
|
total_sum += num1 * num2
|
|
elif match.group(0) == 'do()':
|
|
mul_enabled = True
|
|
elif match.group(0) == "don't()":
|
|
mul_enabled = False
|
|
|
|
return total_sum
|
|
print(process_instructions(input))
|