39 lines
899 B
Python
Executable File
39 lines
899 B
Python
Executable File
#!/usr/bin/env python
|
|
import re
|
|
|
|
from common import parse
|
|
|
|
|
|
def solve(input: str, debug=False) -> int:
|
|
regex = r"(mul\((\d+),(\d+)\)|do\(\)|don't\(\))"
|
|
prog = re.findall(regex, input)
|
|
the_sum = 0
|
|
enabled = True
|
|
for inst in prog:
|
|
if debug:
|
|
print(inst[0], end=": ")
|
|
if enabled and inst[0].startswith("mul"):
|
|
if debug:
|
|
print("multiplying")
|
|
the_sum += int(inst[1]) * int(inst[2])
|
|
elif enabled and inst[0] == "don't()":
|
|
if debug:
|
|
print("disabling")
|
|
enabled = False
|
|
elif not enabled and inst[0] == "do()":
|
|
if debug:
|
|
print("enabling")
|
|
enabled = True
|
|
else:
|
|
if debug:
|
|
print("noop")
|
|
return the_sum
|
|
|
|
|
|
def main():
|
|
print(solve(parse("input")))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|