2023 - Day 1 (Trebuchet?!)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3

#Advent of Code
#2023 Day 1, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input: list[str]) -> int:
    def parse_calibration_value(line: str) -> int:
        first, last = "", ""
        for c in line:
            if c.isdigit():
                first, last = first or c, c
        return int(f"{first}{last}")

    return sum(map(parse_calibration_value, puzzle_input))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The sum of all the calibration values is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["1abc2",
                                       "pqr3stu8vwx",
                                       "a1b2c3d4e5f",
                                       "treb7uchet"]), 142)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
#!/usr/bin/env python3

#Advent of Code
#2023 Day 1, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

import re

def solve(puzzle_input: list[str]) -> int:
    valid_digits: re.Pattern = re.compile(r"\d|one|two|three|four|five|six|seven|eight|nine")

    def digit_to_int(digit: str) -> int:
        match digit:
            case "1" | "one":   return 1
            case "2" | "two":   return 2
            case "3" | "three": return 3
            case "4" | "four":  return 4
            case "5" | "five":  return 5
            case "6" | "six":   return 6
            case "7" | "seven": return 7
            case "8" | "eight": return 8
            case "9" | "nine":  return 9
    def parse_calibration_value(line: str) -> int:
        first = 0

        # Originally implemented with a re.findall(), but since numbers
        # may be overlapping (e.g. "oneight" contains both "one" and "eight")
        # and we want the last digit, we can't use this.
        #
        # Then implemented using a list of valid number strings
        # and line[i:].startswith() scanning forwards through each line.
        #
        # Finally, refactored to use a regex matching valid numbers, and scanning
        # forwards through each line to find the first digit followed by scanning
        # backwards to find the last digit (inspired by a friend's solution -
        # @DavidDwittyy on Twitter) to reduce the runtime from 10s of milliseconds to <10.

        for i in range(len(line)):
            if digit := valid_digits.match(line[i:]):
                first = digit[0]
                break

        for i in range(len(line)-1, -1, -1):
            if digit := valid_digits.match(line[i:]):
                return (10 * digit_to_int(first)) + digit_to_int(digit[0])


    return sum(map(parse_calibration_value, puzzle_input))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The sum of all the calibration values is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["two1nine",
                                       "eightwothree",
                                       "abcone2threexyz",
                                       "xtwone3four",
                                       "4nineeightseven2",
                                       "zoneight234",
                                       "7pqrstsixteen"]), 281)

if __name__ == "__main__":
    run(main)
Return to solution index