This commit is contained in:
2023-12-02 14:09:24 +01:00
parent a131ef4715
commit b2792596f9

View File

@@ -2,27 +2,34 @@ from aocd.models import Puzzle
from aocd import submit from aocd import submit
def calibrate(data: list) -> int: def calibrate(line: list) -> int:
""" """
Solve part a. Solve part a.
- Filter all digits from the line
- Add the first and the last and return result
""" """
# filter digits # filter digits
digits = [c for c in data if c.isdigit()] digits = [c for c in line if c.isdigit()]
# return the sum of the first and the last digit # return the sum of the first and the last digit
return int(digits[0] + digits[-1]) return int(digits[0] + digits[-1])
def translate(s: str) -> str: def translate(line: str) -> str:
""" """
Solve part b. Solve part b.
- Look for spelled out numbers
- replace them by a string where the first and last digit is preserved
- due to the fact that numbers can be part of each other
- return the translation and use the calibratoin function of part a.
""" """
nums = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
conv = ["o1e", "t2o", "t3e", "f4r", "f5e", "s6x", "s7n", "e8t", "n9e"]
for n, c in zip(nums, conv): numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
while n in s: translation = ["o1e", "t2o", "t3e", "f4r", "f5e", "s6x", "s7n", "e8t", "n9e"]
s = s.replace(n, c)
return s for n, t in zip(numbers, translation):
while n in line:
line = line.replace(n, t)
return line
if __name__ == "__main__": if __name__ == "__main__":