74 lines
2.1 KiB
Python
74 lines
2.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, ...]] = []
|
||
|
self.sanitzed_vals: List[Tuple[str, str]] = []
|
||
|
self.clean_vals: List[Tuple[int, int]] = []
|
||
|
self.pattern: str = r"mul\((-?\d+),(-?\d+)\)|(do\(\)|don\'t\(\))"
|
||
|
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 sanitze_matches(self):
|
||
|
active: bool = True
|
||
|
#print(self.vals)
|
||
|
#return
|
||
|
for reg_tuple in self.vals:
|
||
|
# if status == true and it's a mul expression
|
||
|
#print(reg_tuple)
|
||
|
if active and ((reg_tuple[2] != 'do()') and (reg_tuple[2] != "don't()")):
|
||
|
val1: str = reg_tuple[0]
|
||
|
val2: str = reg_tuple[1]
|
||
|
tmp_tuple: Tuple[str, str] = (val1, val2)
|
||
|
self.sanitzed_vals.append(tmp_tuple)
|
||
|
|
||
|
elif active == False and (reg_tuple[2] == 'do()'):
|
||
|
active = True
|
||
|
|
||
|
elif active and (reg_tuple[2] == "don't()"):
|
||
|
active = False
|
||
|
|
||
|
|
||
|
def clean_matches(self) -> None:
|
||
|
#print(self.sanitzed_vals)
|
||
|
#return
|
||
|
for pair in self.sanitzed_vals:
|
||
|
if pair[0] == "":
|
||
|
tmp_tuple: Tuple[int, int] = (0, 0)
|
||
|
pair = tmp_tuple
|
||
|
|
||
|
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():
|
||
|
solution: Solver = Solver()
|
||
|
solution.read()
|
||
|
solution.get_matches()
|
||
|
solution.sanitze_matches()
|
||
|
#return
|
||
|
solution.clean_matches()
|
||
|
#return
|
||
|
result = solution.calculate_result()
|
||
|
print(result)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|