Solve day 3 part 1
This commit is contained in:
parent
1a1e6c18f0
commit
06c7f55681
40
day_3.cpp
Normal file
40
day_3.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user