AdventofCode2024/Day3/solution1.py
Emma Nora Theuer 122c4272f4 Initial Commit
2024-12-09 08:54:41 +01:00

43 lines
1.1 KiB
Python

#!/usr/bin/env python3
import re
from typing import List, Tuple
class Solver:
def __init__(self) -> None:
self.input: str = ""
self.vals: List[Tuple[str, str]] = []
self.clean_vals: List[Tuple[int, int]] = []
self.pattern: str = r"mul\((-?\d+),(-?\d+)\)"
self.result: int = 0
def read(self) -> None:
with open("input") as f:
self.input = f.read()
def get_matches(self) -> None:
self.vals = re.findall(self.pattern, self.input)
def clean_matches(self) -> None:
for pair in self.vals:
val1: int = int(pair[0])
val2: int = int(pair[1])
clean_tuple: Tuple[int, int] = (val1, val2)
self.clean_vals.append(clean_tuple)
def calculate_result(self) -> int:
for pair in self.clean_vals:
tmp: int = pair[0] * pair[1]
self.result += tmp
return self.result
def main() -> None:
solution: Solver = Solver()
solution.read()
solution.get_matches()
solution.clean_matches()
result: int = solution.calculate_result()
print(result)
if __name__ == "__main__":
main()