Finally working for one case.
This commit is contained in:
		
					parent
					
						
							
								c1e6105e0a
							
						
					
				
			
			
				commit
				
					
						d7bf213e7b
					
				
			
		
					 3 changed files with 65 additions and 6 deletions
				
			
		| 
						 | 
					@ -7,8 +7,7 @@ find_package(PkgConfig REQUIRED)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pkg_check_modules(Jemalloc REQUIRED jemalloc)
 | 
					pkg_check_modules(Jemalloc REQUIRED jemalloc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set(LINKER_OPTIONS
 | 
					set(LINKER_OPTIONS -Wl,--sort-common,--as-needed,--gc-sections,--strip-all)
 | 
				
			||||||
    PRIVATE -Wl,--sort-common,--as-needed,--gc-sections,--strip-all)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
set(COMPILE_FLAGS
 | 
					set(COMPILE_FLAGS
 | 
				
			||||||
    -pipe
 | 
					    -pipe
 | 
				
			||||||
| 
						 | 
					@ -52,7 +51,9 @@ set_target_properties(AdventOfCode2023 pb_1 PROPERTIES
 | 
				
			||||||
                      INTERPROCEDURAL_OPTIMIZATION ON
 | 
					                      INTERPROCEDURAL_OPTIMIZATION ON
 | 
				
			||||||
                      UNITY_BUILD ON
 | 
					                      UNITY_BUILD ON
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					target_compile_definitions(AdventOfCode2023 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
 | 
				
			||||||
 | 
					target_compile_definitions(pb_1 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
 | 
				
			||||||
target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_OPTIONS})
 | 
					target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_OPTIONS})
 | 
				
			||||||
target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
 | 
					target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
 | 
				
			||||||
target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${Jemalloc_LIBRARIES})
 | 
					target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${Jemalloc_LIBRARIES})
 | 
				
			||||||
target_link_libraries(pb_1 ${Jemalloc_LIBRARIES})
 | 
					target_link_libraries(pb_1 PUBLIC ${Jemalloc_LIBRARIES})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
 | 
					abcone2threexyz
 | 
				
			||||||
two1nine
 | 
					two1nine
 | 
				
			||||||
eightwothree
 | 
					eightwothree
 | 
				
			||||||
abcone2threexyz
 | 
					 | 
				
			||||||
xtwone3four
 | 
					xtwone3four
 | 
				
			||||||
4nineeightseven2
 | 
					4nineeightseven2
 | 
				
			||||||
zoneight234
 | 
					zoneight234
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,6 +4,10 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "problem_1.hpp"
 | 
					#include "problem_1.hpp"
 | 
				
			||||||
#include <array>
 | 
					#include <array>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <string_view>
 | 
				
			||||||
 | 
					#include <optional>
 | 
				
			||||||
 | 
					#include <unordered_set>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
#include <fstream>
 | 
					#include <fstream>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
| 
						 | 
					@ -26,6 +30,18 @@ namespace pb1 {
 | 
				
			||||||
            "nine"sv,
 | 
					            "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::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept {
 | 
				
			||||||
        std::ifstream file(problemFile);
 | 
					        std::ifstream file(problemFile);
 | 
				
			||||||
        if (!file.is_open()) {
 | 
					        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 {
 | 
					    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());
 | 
					        line_with_numbers.reserve(toSearch.size());
 | 
				
			||||||
 | 
					        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;
 | 
				
			||||||
 | 
					            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