0
0

New way of doing things

This commit is contained in:
Pcornat 2023-12-07 19:43:05 +01:00
parent 109d333341
commit 425f2dd052
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD

View File

@ -7,7 +7,7 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <optional> #include <optional>
#include <unordered_set> #include <unordered_map>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -30,16 +30,16 @@ namespace pb1 {
"nine"sv, "nine"sv,
}; };
const std::unordered_set numbersSet{ const std::unordered_map<std::string_view, char> numbersSet{
"one"sv, { "one"sv, '1' },
"two"sv, { "two"sv, '2' },
"three"sv, { "three"sv, '3' },
"four"sv, { "four"sv, '4' },
"five"sv, { "five"sv, '5' },
"six"sv, { "six"sv, '6' },
"seven"sv, { "seven"sv, '7' },
"eight"sv, { "eight"sv, '8' },
"nine"sv, { "nine"sv, '9' },
}; };
std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept { std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept {
@ -97,9 +97,10 @@ namespace pb1 {
word.reserve(sizeof("seven") - 1); word.reserve(sizeof("seven") - 1);
for (int i = 0; i < toSearch.size(); ++i) { for (int i = 0; i < toSearch.size(); ++i) {
const int second_letter = i + 1, third = i + 2, fourth = i + 3, fifth = i + 4, sixth = i + 5; const int second_letter = i + 1, third = i + 2, fourth = i + 3, fifth = i + 4, sixth = i + 5;
const std::uint32_t nb_characters_remaining = toSearch.size() - i - 1;
switch (toSearch[i]) { switch (toSearch[i]) {
case 'o': { case 'o': {
if (second_letter >= toSearch.size() or third >= toSearch.size()) { if (nb_characters_remaining < digitLetters[0].size()) {
line_with_numbers.append(toSearch.substr(i)); line_with_numbers.append(toSearch.substr(i));
continue; continue;
} }
@ -109,11 +110,8 @@ namespace pb1 {
if (pos != std::string::npos) { if (pos != std::string::npos) {
line_with_numbers.push_back(word[pos]); line_with_numbers.push_back(word[pos]);
} }
if (word[1] == 'n') {
++i;
}
} else { } else {
line_with_numbers.push_back('1'); line_with_numbers.push_back(numbersSet.at(word));
++i; ++i;
} }
word.clear(); word.clear();