From 3ae174bcfbcf4adfd665fea7e19e5796cafc5566 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 7 Dec 2024 16:41:20 -0800 Subject: [PATCH 1/3] cleanup7 --- y_2024/day7.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/y_2024/day7.py b/y_2024/day7.py index 4168df0..3d48f40 100644 --- a/y_2024/day7.py +++ b/y_2024/day7.py @@ -17,9 +17,6 @@ def conc(a, b): return int(str(a) + str(b)) -OPS = [add, mul] - - @dataclass class Row: original: str @@ -41,12 +38,9 @@ def do(self, lst, ops): rex = [] for i in ops: - l_i = [i(op1, op2)] - l_i.extend(lst[2:]) - rex.append(self.do(l_i, ops)) - if any(rex): - return True - return False + rex.append(self.do([i(op1, op2)] + lst[2:], ops)) + + return any(rex) class Day(AoCDay): @@ -54,21 +48,12 @@ def __init__(self, test=0): super().__init__(__name__, test) def _preprocess_input(self): - print(f"{self._input_data=}") - print(f"{len(self._input_data)=}") - print(f"{len(self._input_data[0])=}") self.__input_data = [Row(i) for j in self._input_data for i in j] def _calculate_1(self): - result = 0 - for x in self.__input_data: - if x.do(x.op_list, [add, mul]): - result += x.result - return result + return sum(x.result for x in self.__input_data if x.do(x.op_list, [add, mul])) def _calculate_2(self): - result = 0 - for x in self.__input_data: - if x.do(x.op_list, [add, mul, conc]): - result += x.result - return result + return sum( + x.result for x in self.__input_data if x.do(x.op_list, [add, mul, conc]) + ) From fa5d5305e6d043d43fe3de89e4e93da2eb4de8d6 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 7 Dec 2024 16:48:09 -0800 Subject: [PATCH 2/3] cleanup 7 --- y_2024/day7.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/y_2024/day7.py b/y_2024/day7.py index 3d48f40..2ac8ac2 100644 --- a/y_2024/day7.py +++ b/y_2024/day7.py @@ -27,20 +27,11 @@ def __post_init__(self) -> None: self.result = int(self.original.split(": ")[0]) self.op_list = [int(i) for i in self.original.split(": ")[1].split(" ")] - def do(self, lst, ops): + def do(self, lst: list[int], ops: list) -> bool: if len(lst) == 1: - if lst[0] == self.result: - return True - return False + return lst[0] == self.result - op1 = lst[0] - op2 = lst[1] - - rex = [] - for i in ops: - rex.append(self.do([i(op1, op2)] + lst[2:], ops)) - - return any(rex) + return any([self.do([op(*lst[:2])] + lst[2:], ops) for op in ops]) class Day(AoCDay): From 8d1a80879159191d68a67f4e09731fe9a3a96c3b Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 7 Dec 2024 16:48:53 -0800 Subject: [PATCH 3/3] cleanup 7 --- y_2024/day7.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/y_2024/day7.py b/y_2024/day7.py index 2ac8ac2..7f2c5b2 100644 --- a/y_2024/day7.py +++ b/y_2024/day7.py @@ -1,19 +1,19 @@ -from typing import Optional +from typing import Optional, Callable from pydantic.dataclasses import dataclass from common.aoc import AoCDay -def add(a, b): +def add(a: int, b: int) -> int: return a + b -def mul(a, b): +def mul(a: int, b: int) -> int: return a * b -def conc(a, b): +def conc(a: int, b: int) -> int: return int(str(a) + str(b)) @@ -27,7 +27,7 @@ def __post_init__(self) -> None: self.result = int(self.original.split(": ")[0]) self.op_list = [int(i) for i in self.original.split(": ")[1].split(" ")] - def do(self, lst: list[int], ops: list) -> bool: + def do(self, lst: list[int], ops: list[Callable]) -> bool: if len(lst) == 1: return lst[0] == self.result