Count The Digit (AoC 2023 Day 1)

Count The Digit (AoC 2023 Day 1)

After almost a year, here is the new post of Advent of Code.

For those who don’t know what is Advent of Code, you can read about it from my last year post.

I failed to write the posts for all problem from last year. The biggest reason is because I’m too lazy to write it, hehe. So for this year I will try different approach by posting everyday after I successfully on that day.

The good news is I successfully solved 24 of 25 problems from last year. There is only one problem that I can’t solve. By the time the year has change to 2023, I’m already too lazy to try to solve it again.

Hopefully this year I can solve all of the problem and write post about all of them.

Day 1: Trebuchet?!

Full problem statement

The first problem of the year should be an easy one.

For each line of a string, find the first and last digit in that string, then combine them to form a single two-digit number. What is the sum of all those numbers?

For part one, the digit is just a character in the string that is a number between 0-9.

I only need to check each character in the string is a number or not to get the digit.

Part two is the tricky one, the digit can also be spelled with letters. So the words one, two, three, four, five, six, seven, eight, and nine are also a digit.

I just create a map of those words into its number. Then I check the substring that start from each position of character of the string is one of those words or not. If it is, then use the map to get the digit from that word.

Solution Code


That’s it for day one.

The problem is quite easy but the implementation is a bit tricky. I spend most of the time thinking how to implement the second part. I also try to remember how to code in C++ because it’s been so long that I code with it.

Day 1 Stats
Start late and code too slow

I started late in about 30 minutes after the puzzle is unlocked. But the result it’s not too bad.

Let’s see if next problem is tricky to implement or not.