-
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 #217 from Stegallo/2023
2023
- Loading branch information
Showing
3 changed files
with
60 additions
and
16 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
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
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,43 @@ | ||
from common.aoc import AoCDay | ||
from typing import List | ||
|
||
SPELLED = [ | ||
"one", | ||
"two", | ||
"three", | ||
"four", | ||
"five", | ||
"six", | ||
"seven", | ||
"eight", | ||
"nine", | ||
] | ||
|
||
|
||
class Day(AoCDay): | ||
def __init__(self, test=0): | ||
super().__init__(__name__, test) | ||
|
||
def _preprocess_input(self): | ||
self.__input_data = self._input_data[0] | ||
|
||
@staticmethod | ||
def replace_word_with_number(input: str) -> str: | ||
for c, k in enumerate(SPELLED): | ||
input = input.replace(k, k + str(c + 1) + k) | ||
return input | ||
|
||
@staticmethod | ||
def calculate_calibration(input: List[str]) -> int: | ||
s = 0 | ||
for i in input: | ||
j = [x for x in i if x.isnumeric()] | ||
s += int(f"{j[0]}{j[-1]}") | ||
return s | ||
|
||
def _calculate_1(self) -> int: | ||
return self.calculate_calibration(self.__input_data) | ||
|
||
def _calculate_2(self) -> int: | ||
new_input = [self.replace_word_with_number(i) for i in self.__input_data] | ||
return self.calculate_calibration(new_input) |