Skip to content

Commit

Permalink
Merge pull request #243 from Stegallo/2023
Browse files Browse the repository at this point in the history
2023
  • Loading branch information
Stegallo authored Dec 18, 2023
2 parents acf441a + e6e8180 commit 32eb27f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions y_2023/day9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List, Any

from pydantic.dataclasses import dataclass

from common.aoc import AoCDay


@dataclass
class History:
vals: List[int]

@property
def next(self) -> Any:
return History(
[
int(self.vals[i + 1]) - int(self.vals[i])
for i in range(len(self.vals) - 1)
],
)

def next_value(self) -> int:
if set(self.vals) == {0}:
return 0
self.vals.append(self.vals[-1] + self.next.next_value())
return self.vals[-1]

def prev_value(self) -> int:
if set(self.vals) == {0}:
return 0
self.vals.insert(0, self.vals[0] - self.next.prev_value())
return self.vals[0]


class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
self.__input_data = [History(i.split(" ")) for i in self._input_data[0]]

def _calculate_1(self) -> int: # 2043183816
return sum(x.next_value() for x in self.__input_data)

def _calculate_2(self) -> int: # 1118
return sum(x.prev_value() for x in self.__input_data)

0 comments on commit 32eb27f

Please sign in to comment.