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