Skip to content

Commit

Permalink
Merge pull request #208 from Stegallo/2015
Browse files Browse the repository at this point in the history
day10
  • Loading branch information
Stegallo authored Nov 14, 2023
2 parents 32ecf6e + 13d2280 commit b87e97e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 8 additions & 3 deletions y_2015/day10.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from common.aoc import AoCDay
from typing import List


class Day(AoCDay):
Expand All @@ -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)):
Expand All @@ -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)

Expand Down
12 changes: 4 additions & 8 deletions y_2015/day7.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand All @@ -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
Expand Down

0 comments on commit b87e97e

Please sign in to comment.