Finally working for one case.
This commit is contained in:
		
					parent
					
						
							
								c1e6105e0a
							
						
					
				
			
			
				commit
				
					
						d7bf213e7b
					
				
			
		
					 3 changed files with 65 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -4,6 +4,10 @@
 | 
			
		|||
 | 
			
		||||
#include "problem_1.hpp"
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <string_view>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <unordered_set>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +30,18 @@ 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,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept {
 | 
			
		||||
        std::ifstream file(problemFile);
 | 
			
		||||
        if (!file.is_open()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -76,10 +92,52 @@ namespace pb1 {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    void part2(const std::string_view &toSearch, std::vector<std::string> &splittedLine, std::size_t &result) noexcept {
 | 
			
		||||
        std::string line_with_numbers;
 | 
			
		||||
        std::string line_with_numbers, word;
 | 
			
		||||
        line_with_numbers.reserve(toSearch.size());
 | 
			
		||||
        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;
 | 
			
		||||
            switch (toSearch[i]) {
 | 
			
		||||
                case 'o': {
 | 
			
		||||
                    if (second_letter >= toSearch.size() or third >= toSearch.size()) {
 | 
			
		||||
                        line_with_numbers.append(toSearch.substr(i));
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    word.append(toSearch.substr(i, digitLetters[0].size()));
 | 
			
		||||
                    if (!numbersSet.contains(word)) {
 | 
			
		||||
                        const auto pos = word.find_first_of(digits);
 | 
			
		||||
                        if (pos != std::string::npos) {
 | 
			
		||||
                            line_with_numbers.push_back(word[pos]);
 | 
			
		||||
                        }
 | 
			
		||||
                        if (word[1] == 'n') {
 | 
			
		||||
                            ++i;
 | 
			
		||||
                        }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        line_with_numbers.push_back('1');
 | 
			
		||||
                        ++i;
 | 
			
		||||
                    }
 | 
			
		||||
                    word.clear();
 | 
			
		||||
                }
 | 
			
		||||
                    break;
 | 
			
		||||
                case 't':
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'f':
 | 
			
		||||
                    break;
 | 
			
		||||
                case 's':
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'e':
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'n':
 | 
			
		||||
                    break;
 | 
			
		||||
                default:
 | 
			
		||||
                    if ((toSearch[i] - '0') < 10) {
 | 
			
		||||
                        line_with_numbers.push_back(toSearch[i]);
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (!line_with_numbers.empty()) {
 | 
			
		||||
            result += (line_with_numbers.front() - '0') * 10 + (line_with_numbers.back() - '0');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue