def run(): print(f"trebuchet total: {trebuchet(r'./src/assets/day_1.txt')}") print(f"spelled trebuchet total: {spelled_trebuchet(r'./src/assets/day_1.txt')}") def trebuchet(path): total = 0 with open(path) as f: lines = f.readlines() total = count_calibration_lines(lines) return total def spelled_trebuchet(path): dictionary = {"one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9", "ten": "10"} total = 0 with open(path) as f: lines = f.readlines() for line in lines: new_line = line # FIXME: wrong for key,value in dictionary.items(): new_line = new_line.replace(key, value) print(new_line) total += count_calibration(new_line) return total def count_calibration_lines(lines): total = 0 for line in lines: total += count_calibration(line) return total def count_calibration(line): init = None end = None for c in line: try: next = int(c) if init is None: init = next else: end = next except: pass if end is None: end = init if init is not None and end is not None: line_value = init * 10 + end return line_value return 0