diff --git a/1/trebuchet.py b/1/trebuchet.py index 1012870..5ee62df 100644 --- a/1/trebuchet.py +++ b/1/trebuchet.py @@ -2,27 +2,34 @@ from aocd.models import Puzzle from aocd import submit -def calibrate(data: list) -> int: +def calibrate(line: list) -> int: """ Solve part a. + - Filter all digits from the line + - Add the first and the last and return result """ # 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 int(digits[0] + digits[-1]) -def translate(s: str) -> str: +def translate(line: str) -> str: """ 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): - while n in s: - s = s.replace(n, c) - return s + numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] + translation = ["o1e", "t2o", "t3e", "f4r", "f5e", "s6x", "s7n", "e8t", "n9e"] + + for n, t in zip(numbers, translation): + while n in line: + line = line.replace(n, t) + return line if __name__ == "__main__":