Skip to content

Commit

Permalink
Merge pull request #254 from Stegallo/2024
Browse files Browse the repository at this point in the history
cleanup7
  • Loading branch information
Stegallo authored Dec 8, 2024
2 parents c47ee21 + 8d1a808 commit 944caed
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions y_2024/day7.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
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))


OPS = [add, mul]


@dataclass
class Row:
original: str
Expand All @@ -30,45 +27,24 @@ 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[Callable]) -> bool:
if len(lst) == 1:
if lst[0] == self.result:
return True
return False

op1 = lst[0]
op2 = lst[1]
return lst[0] == self.result

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
return any([self.do([op(*lst[:2])] + lst[2:], ops) for op in ops])


class Day(AoCDay):
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])
)

0 comments on commit 944caed

Please sign in to comment.