From c0a5c2165dc1d396d978bc21a352bd64226bcb7a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 3 Dec 2024 10:12:55 +0000 Subject: [PATCH] 2024: d03: ex1: add solution --- 2024/d03/ex1/ex1.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 2024/d03/ex1/ex1.py diff --git a/2024/d03/ex1/ex1.py b/2024/d03/ex1/ex1.py new file mode 100755 index 0000000..1981c7b --- /dev/null +++ b/2024/d03/ex1/ex1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import sys +import re +import dataclasses + + +@dataclasses.dataclass +class MulInstruction: + lhs: int + rhs: int + + def calc(self) -> int: + return self.lhs * self.rhs + + +def solve(input: str) -> int: + def parse(input: str) -> list[MulInstruction]: + res: list[MulInstruction] = [] + + MUL_REGEX = re.compile(r"mul\((\d+),(\d+)\)") + for match in MUL_REGEX.finditer(input): + res.append(MulInstruction(int(match.group(1)), int(match.group(2)))) + + return res + + return sum(inst.calc() for inst in parse(input)) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()