Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2023 Day 1
- Advent of Code 2023 Day 2
- Advent of Code 2023 Day 3
- Advent of Code 2023 Day 4
- Advent of Code 2023 Day 5
- Advent of Code 2023 Day 6
- Advent of Code 2023 Day 7
- Advent of Code 2023 Day 8
- Advent of Code 2023 Day 9
- Advent of Code 2023 Day 10
- Advent of Code 2023 Day 11
- Advent of Code 2023 Day 12
- Advent of Code 2023 Day 13
- Advent of Code 2023 Day 14
- Advent of Code 2023 Day 15
- Advent of Code 2023 Day 16
- Advent of Code 2023 Day 17
- Advent of Code 2023 Day 18
- Advent of Code 2023 Day 19
- Advent of Code 2023 Day 20
- Advent of Code 2023 Day 21
- Advent of Code 2023 Day 22
- Advent of Code 2023 Day 23
- Advent of Code 2023 Day 25
-
Older post
-
Newer post
Advent of Code 2023 Day 1
Day 1: Trebuchet?!
https://adventofcode.com/2023/day/1
Something is wrong with the global snow production and you are the lucky fixer! (yay?)
Snow comes from the sky, and the sky is where you are going. The vehicle of choice to get to the destination is a trebuchet.
Today’s input is a list of calibration instructions for that trebuchet.
An example input looks like this:
Part 1
Each line of input holds a calibration value. A calibration value is a two-digit number.
It can be constructed by concatenating the first digit in a line with the last digit in that line. The question asks for sum of all calibration values in the input.
Option 1: A for loop
Some skeleton/pseudo-code to start with:
I chose to not overthink this one and to be very verbose on purpose.
Starting to search from the front of the line, I look for the first character that is a digit. Then I turn that into an integer so I can do math on it.
To find the last digit, I do the same thing starting from the back of the line.
When I found both digits, I merge them into a two-digit number and add that number to the final sum.
Option 2: An iterator chain
The exact same idea, but implemented a bit differently.
Split the input into lines. Turn each line into a number. Sum those numbers.
Code
Part 2
It turns out spelled out digits are also valid.
So “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, and “nine” also count as valid “digits”,
with values from 1 to 9 respectively.
Input lines can look like this:
For example, the first digit on the first line would be "two"
, with a numeric value of 2
.
The last digit on that line is "nine"
, with a value of 9
.
That means the calibration value for that line is 29
.
This complicates things, because you can’t look at a line 1 character at a time like in part 1.
The skeleton code remains unchanged from part 1.
To find the first digit, I create a temporary line that starts off as the entire line. If it starts with a valid prefix, the digit was found. If it does not, I shorten that temporary variable so it starts one character later and do the check again.
To find the last digit, I apply the same logic with 2 changes:
- I check if the temporary line ends with a valid suffix.
- I shorten that temporary line by removing one character from the end.
In pseudocode that looks like: