From 06c7f55681e6985c1fc8dd4e5d796ad8aae052a3 Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 17 Dec 2025 12:09:48 +0100 Subject: [PATCH] Solve day 3 part 1 --- day_3.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 day_3.cpp diff --git a/day_3.cpp b/day_3.cpp new file mode 100644 index 0000000..5c441a9 --- /dev/null +++ b/day_3.cpp @@ -0,0 +1,40 @@ +#include +#include + +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; +}