adventofcode-2025/day_3.cpp
2025-12-17 12:28:48 +01:00

45 lines
1001 B
C++

#include <fstream>
#include <iostream>
int main()
{
std::ifstream file("input.txt");
long long int joltage_sum = 0;
const int digit_count = 2; // Use 12 for part 2
std::string bank;
while (getline(file, bank)) {
int indices[digit_count];
for (int digit_index = 0; digit_index < digit_count; digit_index++) {
for (int i = 9; i > 0; i--) {
char digit = i + 48;
// Start from previous index
int start_pos = digit_index == 0 ? 0 : indices[digit_index - 1] + 1;
int index = bank.find(digit, start_pos);
if (index < bank.size() - digit_count + digit_index + 1 && index != std::string::npos) {
indices[digit_index] = index;
break;
}
}
}
// Collect characters into a single number
std::string joltage_str;
for (int index : indices) {
joltage_str += bank[index];
}
long long int joltage = std::stoll(joltage_str);
std::cout << joltage << std::endl;
joltage_sum += joltage;
}
std::cout << "\nSum: " << joltage_sum << std::endl;
}