#!/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 stance in prog: if debug: print(stance[0], end=": ") if enabled and stance[0].startswith("mul"): if debug: print("multiplying") the_sum += int(stance[1]) * int(stance[2]) elif enabled and stance[0] == "don't()": if debug: print("disabling") enabled = False elif not enabled and stance[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()