Advent-Of-Code-2024/q3.py

34 lines
785 B
Python
Raw Permalink Normal View History

2024-12-06 05:13:07 +00:00
import re
MUL_PATTERN = re.compile(r"mul\((\d+), *(\d+)\)")
def task1(text: str) -> int:
total = 0
for mul in MUL_PATTERN.finditer(text):
a, b = map(int, mul.groups())
total += a*b
return total
MUL_OR_DO_PATTERN = re.compile(r"do\(\)|don't\(\)|mul\((\d+), *(\d+)\)")
def task2(text: str) -> int:
total = 0
on = True
for op in MUL_OR_DO_PATTERN.finditer(text):
if op.group(0) == "do()":
on = True
if op.group(0) == "don't()":
on = False
if op.group(1) and on:
a, b = map(int, op.groups())
total += a*b
return total
def main():
with open("i3.txt") as f:
text = f.read()
print(task1(text))
print(task2(text))
if __name__ == "__main__":
main()