solve day 01 part 2

This commit is contained in:
Heiko Ludwig 2023-12-01 21:36:24 +01:00
parent d270ffbf1a
commit d8fcaedfda
5 changed files with 59 additions and 6 deletions

View file

@ -2,7 +2,17 @@
# https://adventofcode.com/2023/day/1
import re
NUMBER_DICT = {
"one": "on1e",
"two": "tw2o",
"three": "thr3ee",
"four": "fo4ur",
"five": "fi5ve",
"six": "si6x",
"seven": "sev7en",
"eight": "ei8ght",
"nine": "ni9ne",
}
def get_lines(filename: str) -> list:
@ -26,13 +36,28 @@ def get_cal_val(line: str) -> int:
return int(first + last)
def main():
# lines = get_lines("test-input.txt")
lines = get_lines("input.txt")
def replace_digit_strings(line: str) -> str:
for digit in NUMBER_DICT.keys():
line = line.replace(digit, NUMBER_DICT[digit])
return line
def get_sum_cal_val(lines: list, spelled_out: bool) -> int:
sum_cal_val = 0
for line in lines:
sum_cal_val += get_cal_val(line)
print(f"Part 1: The sum of all calibration values is {sum_cal_val}.")
if spelled_out:
sum_cal_val += get_cal_val(replace_digit_strings(line))
else:
sum_cal_val += get_cal_val(line)
return sum_cal_val
def main():
lines = get_lines("input.txt")
sum_cal_val = get_sum_cal_val(lines, False)
print(f"Part 1: The sum of all calibration values is: {sum_cal_val}")
sum_cal_val = get_sum_cal_val(lines, True)
print(f"Part 2: The sum of all calibration values is: {sum_cal_val}")
if __name__ == '__main__':