diff --git a/y_2015/day10.py b/y_2015/day10.py index 5d49e76..8f2bfd9 100644 --- a/y_2015/day10.py +++ b/y_2015/day10.py @@ -1,4 +1,5 @@ from common.aoc import AoCDay +from typing import List class Day(AoCDay): @@ -8,9 +9,7 @@ def __init__(self, test=0): def _preprocess_input(self): self.__input_data = self._input_data[0][0] - def say_loud(self, x: str) -> str: - if len(x) == 1: - return f"1{x[0]}" + def _chunkify(self, x: str) -> List[List[str]]: chunks = [] chunk = [x[0]] for c in range(len(x)): @@ -23,6 +22,12 @@ def say_loud(self, x: str) -> str: chunks.append(chunk) chunk = [x[c]] chunks.append(chunk) + return chunks + + def say_loud(self, x: str) -> str: + if len(x) == 1: + return f"1{x[0]}" + chunks = self._chunkify(x) k = [str(len(i)) + i[0] for i in chunks] return "".join(k) diff --git a/y_2015/day7.py b/y_2015/day7.py index dd542a7..88c840b 100644 --- a/y_2015/day7.py +++ b/y_2015/day7.py @@ -24,8 +24,7 @@ class Operator: class Element: source: str operator: Optional[Operator] = None - result: int = 0 - result_valid: bool = False + cached_result: Optional[int] = None def __post_init__(self) -> None: for i in list(OPERATORS.keys()): @@ -41,18 +40,15 @@ def __compute_result(self, wires) -> int: int(i) if i.isnumeric() else wires[i].resolve(wires) for i in self.operator.inputs ] - return OPERATORS[self.operator.source](*inputs) if self.source.isnumeric(): return int(self.source) return wires[self.source].resolve(wires) def resolve(self, wires) -> int: - if self.result_valid: - return self.result - self.result = self.__compute_result(wires) - self.result_valid = True - return self.result + if not self.cached_result: + self.cached_result = self.__compute_result(wires) + return self.cached_result @dataclass