From 5e9b5a3a8f6280609b6759b4934386bdbc68d2d1 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Tue, 3 Dec 2024 09:21:41 -0500 Subject: [PATCH] refactor: second solution for 2024/3 to use dataclasses --- 2024/3/2.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/2024/3/2.py b/2024/3/2.py index 18f6d43..a6c1a6a 100644 --- a/2024/3/2.py +++ b/2024/3/2.py @@ -1,11 +1,24 @@ import fileinput import re -from collections import namedtuple +from dataclasses import dataclass from fileinput import FileInput -MulInstruction = namedtuple("MulInstruction", ["pos", "x", "y"]) -DoInstruction = namedtuple("DoInstruction", ["pos"]) -DontInstruction = namedtuple("DontInstruction", ["pos"]) + +@dataclass +class MulInstruction: + pos: int + x: int + y: int + + +@dataclass +class DoInstruction: + pos: int + + +@dataclass +class DontInstruction: + pos: int def handler(raw_instructions: FileInput) -> int: @@ -19,7 +32,7 @@ def handler(raw_instructions: FileInput) -> int: matches_do = list(re.finditer(pattern_do, all_instructions)) matches_dont = list(re.finditer(pattern_dont, all_instructions)) - instructions = [] + instructions: list[MulInstruction | DoInstruction | DontInstruction] = [] instructions.extend([MulInstruction(pos=m.start(), x=int(m.group(1)), y=int(m.group(2))) for m in matches_mul]) instructions.extend([DoInstruction(pos=m.start()) for m in matches_do])