Posted on: 2025-12-05, Updated on: 2025-12-05 14:32:36 | Read time: 1.9 minutes
Topic(s): programming
For an introduction to the Advent of Code, checkout my first article for AOC 2025. This article will cover my solution for the first and second parts of Day 3.
NOTE: It should go without saying that this article will contain spoilers for the given day's Advent of Code problem. Continue reading at your own risk!
// Advent of Code 2025 - Day 03 Part 1
#include "AdventOfCodeStd.hpp"
using namespace AOC;
int main(void)
{
using bank_t = std::vector<std::uint8_t>;
std::vector<bank_t> banks;
for (std::string s; std::getline(std::cin, s); )
{
bank_t bank;
for (auto ch : s)
bank.emplace_back(std::stoi(std::string(1, ch)));
banks.emplace_back(bank);
}
std::vector<std::int32_t> joltages;
for (const auto &bank : banks)
{
std::vector<int32_t> maxes;
for (std::size_t i = 0; i < bank.size(); ++i)
{
std::int32_t max{}; // Keep track of the max two-digit combo from this bank
std::uint8_t a = bank[i];
for (std::size_t j = i + 1; j < bank.size(); ++j)
max = std::max(max, (a * 10) + bank[j]);
maxes.emplace_back(max);
}
auto max_elem = std::ranges::max_element(maxes);
std::println("Max for bank {}: {}", bank, *max_elem);
joltages.push_back(*max_elem);
}
std::println("{}", std::ranges::fold_left(joltages, 0, std::plus<>()));
}
For this problem, I placed the input in a vector of bank_t objects which are std::vector<std::uint8_t> where each number represents the digits in the bank. It then iterates through the banks to find the largest combination of two digits for each bank using nested loops. The inner loop iterates an index j past the index i, since the second digit in the combination can only be formed by those that appear after the first. I don't think that is explicitly stated in the problem text, but it can be deduced from the explanation of the example input. Finally, it prints the sum of the collected “joltages,” which is the answer to the problem.
Thanks for reading, and stay tuned for more articles about this year's Advent of Code!
Resources: