-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from Stegallo/2023
2023
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |