import re def parse(raw: str) -> list[tuple[int]]: finds = re.findall(r'do\(\)|don\'t\(\)|mul\([0-9]+,[0-9]+\)', raw) active = True pairs = [] for f in finds: # Active or nah if 'do(' in f: active = True if 'don' in f: active = False if not active or 'mul' not in f: continue # Figure out the mult part parts = f.strip('mul()').split(',') pairs.append((int(parts[0]), int(parts[1]))) return pairs with open('data.input') as file: print(sum(m[0] * m[1] for m in parse(file.read())))