Files
AOC_2024/03b.py
2024-12-10 14:36:10 -06:00

25 lines
523 B
Python
Executable File

#!/usr/bin/python
import re
with open('03a.txt') as f:
text = f.read()
reg = re.compile(r'(?P<mul>mul\((?P<num1>[0-9]+),(?P<num2>[0-9]+)\))|(?P<do>do\(\))|(?P<dont>don\'t\(\))')
total = 0
enabled = True
for match in reg.finditer(text):
if match.group('do'):
enabled = True
elif match.group('dont'):
enabled = False
elif match.group('mul'):
if enabled:
total += int(match.group('num1')) * int(match.group('num2'))
else:
raise RuntimeError()
print(total)