Skip to content

Part 2

Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".

Equipped with this new information, you now need to find the real first and last digit on each line. For example:

two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.

What is the sum of all of the calibration values?

Solution:

word_calibration_sum

word_calibration_sum(text: str) -> int
  • Split input on newline
  • iterate over lines
  • extract word digts
  • convert to integer strings
  • concatenate
  • converte to integers
  • then sum.

Parameters:

Name Type Description Default
text str

Raw input text.

required

Returns:

Type Description
int

sum of all the concatenated first and last word numbers of each line.

Source code in aoc/day1/part2.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def word_calibration_sum(text: str) -> int:
    """
    * Split input on newline
    * iterate over lines
    * extract word digts
    * convert to integer strings
    * concatenate
    * converte to integers
    *  then sum.

    Args:
        text: Raw input text.

    Returns:
        sum of all the concatenated first and last word numbers of each line.

    """
    lines = text.strip().split("\n")
    nums = []
    for line in lines:
        matches: list[tuple[str, ...]] = word_pattern.findall(line)
        digit_words = [word for tup in matches for word in tup if word]
        digit_1 = digit_words[0]
        digit_2 = digit_words[-1]
        if digit_1 in word_to_digit:
            digit_1 = word_to_digit[digit_1]
        if digit_2 in word_to_digit:
            digit_2 = word_to_digit[digit_2]
        nums.append(int("".join([digit_1, digit_2])))
    return sum(nums)