47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from aoc import Aoc2, AocSameImplementation, AocParseLines
|
|
|
|
|
|
class Day01(AocParseLines[str, str], AocSameImplementation[list[str]], Aoc2[list[str], list[str]]):
|
|
def __init__(self) -> None:
|
|
Aoc2.__init__(self, 2023, 1)
|
|
|
|
def replace_digits(self, line: str) -> str:
|
|
spelled_digits = [
|
|
"zero",
|
|
"one",
|
|
"two",
|
|
"three",
|
|
"four",
|
|
"five",
|
|
"six",
|
|
"seven",
|
|
"eight",
|
|
"nine",
|
|
]
|
|
for i, digit in enumerate(spelled_digits):
|
|
line = line.replace(digit, digit[0] + str(i) + digit[-1])
|
|
|
|
return line
|
|
|
|
def filter_digits(self, line: str) -> int:
|
|
digits = list(filter(lambda x: x in map(str, range(10)), line))
|
|
rdigits = list(filter(lambda x: x in map(str, range(10)), reversed(line)))
|
|
return int(digits[0] + rdigits[0])
|
|
|
|
def parseline1(self, inpt: str) -> str:
|
|
return inpt
|
|
|
|
def parseline2(self, inpt: str) -> str:
|
|
return self.replace_digits(inpt)
|
|
|
|
def part(self, inpt: list[str]) -> int:
|
|
return sum(map(self.filter_digits, inpt))
|
|
|
|
|
|
def main() -> None:
|
|
day1 = Day01()
|
|
day1.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|