aoc2024/day03.py

44 lines
1.2 KiB
Python

from tools.aoc import AOCDay
from typing import Any
import re
class Day(AOCDay):
inputs = [
[
(322, "input3_test"), # example contained twice due to the real input having multiple lines
(174561379, "input3_dennis"),
(196826776, "input3"),
],
[
(96, "input3_test2"), # example contained twice due to the real input having multiple lines
(106921067, "input3_dennis"),
(106780429, "input3"),
],
]
def get_matches(self, regex: re.Pattern) -> tuple:
for line in self.getInput():
for match in regex.findall(line):
yield match
def part1(self) -> Any:
return sum(int(match[0]) * int(match[1]) for match in self.get_matches(re.compile(r"mul\((\d+),(\d+)\)")))
def part2(self) -> Any:
ans = 0
do = True
for match in self.get_matches(re.compile(r"((do)\(\)|(don't)\(\)|mul\((\d+),(\d+)\))")):
if match[1] == "do":
do = True
elif match[2] == "don't":
do = False
elif do:
ans += int(match[3]) * int(match[4])
return ans
if __name__ == "__main__":
day = Day(2024, 3)
day.run(verbose=True)