From 9a164c3f65c01e31a9d1a8578e29b358972ddfed Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 1 Dec 2023 21:27:31 +0100 Subject: Lösung für Tag1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/01b.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/01b.cpp (limited to 'src/01b.cpp') diff --git a/src/01b.cpp b/src/01b.cpp new file mode 100644 index 0000000..f87da3a --- /dev/null +++ b/src/01b.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +string +replace_all(string str, const string search, const string with) +{ + for ( ;; ) { + auto it = str.find(search); + if ( it == string::npos ) + break; + str.replace(it, search.length(), with); + } + return str; +} + +int +main() +{ + map map{ + { "0", 0 }, + { "1", 1 }, + { "2", 2 }, + { "3", 3 }, + { "4", 4 }, + { "5", 5 }, + { "6", 6 }, + { "7", 7 }, + { "8", 8 }, + { "9", 9 }, + { "one", 1 }, + { "two", 2 }, + { "three", 3 }, + { "four", 4 }, + { "five", 5 }, + { "six", 6 }, + { "seven", 7 }, + { "eight", 8 }, + { "nine", 9 }, + }; + + fstream input{ "data/01a.txt" }; + + int sum = 0; + for ( string line; input >> line; ) { + string sub; + vector digits; + + for ( auto ch: line ) { + sub += ch; + + for ( const auto& word: map ) { + if ( sub.ends_with(word.first) ) { + digits.push_back(word.second); + } + } + } + + auto first = digits.front(); + auto last = digits.back(); + + sum += (first * 10) + last; + } + cout << sum << endl; +} -- cgit v1.3