41 lines
759 B
C++
41 lines
759 B
C++
#include <fstream>
|
|
#include <iostream>
|
|
|
|
int main()
|
|
{
|
|
std::ifstream file("input.txt");
|
|
|
|
int joltage_sum = 0;
|
|
|
|
std::string bank;
|
|
while (getline(file, bank)) {
|
|
int first_index;
|
|
for (int i = 9; i > 0; i--) {
|
|
char digit = i + 48;
|
|
|
|
first_index = bank.find(digit);
|
|
|
|
if (first_index != bank.size() - 1 && first_index != std::string::npos)
|
|
break;
|
|
}
|
|
|
|
int second_index;
|
|
for (int i = 9; i > 0; i--) {
|
|
char digit = i + 48;
|
|
|
|
second_index = bank.find(digit, first_index + 1);
|
|
|
|
if (second_index != std::string::npos)
|
|
break;
|
|
}
|
|
|
|
int joltage = std::stoi(std::string { bank[first_index], bank[second_index] });
|
|
|
|
std::cout << joltage << std::endl;
|
|
|
|
joltage_sum += joltage;
|
|
}
|
|
|
|
std::cout << "\nSum: " << joltage_sum << std::endl;
|
|
}
|