#!/usr/bin/env python3 import re with open("./2024/inputs/day03.txt") as f: instructions = f.read() r = re.compile("(?:mul\\(([\\d]{1,3}),([\\d]{1,3})\\))", re.MULTILINE) matches = re.findall(r, instructions) multiplication_total = 0 for match in matches: n = int(match[0]) m = int(match[1]) multiplication_total += n * m print(multiplication_total) r2 = re.compile("(?:^|do\\(\\))+?(?:.+?)(?:don't\\(\\)|$)+?", re.DOTALL) matches2 = re.findall(r2, instructions) do_multiplication_total = 0 for match in matches2: mults = re.findall(r, match) for mult in mults: n = int(mult[0]) m = int(mult[1]) do_multiplication_total += n * m print(do_multiplication_total)