From a89288524163c43917bd36015d89fb4e0b2955fc Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 17 Jan 2024 21:02:28 +0100 Subject: [PATCH] Stop PB1, go to 2 --- CMakeLists.txt | 37 ++++++++----- inputs/pb1_p2_sample.txt | 4 +- pb_1/problem_1.cpp | 111 +++++++++++++++++++++++++++++++-------- 3 files changed, 114 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index feb914d..5932557 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ find_package(PkgConfig REQUIRED) pkg_check_modules(Jemalloc REQUIRED jemalloc) -set(LINKER_OPTIONS -Wl,--sort-common,--as-needed,--gc-sections,--strip-all) +set(LINKER_OPTIONS -Wl,--sort-common,--as-needed#[[,--gc-sections,--strip-all]]) set(COMPILE_FLAGS -pipe @@ -18,24 +18,27 @@ set(COMPILE_FLAGS -Wall -Wextra -Wpedantic - -ffunction-sections - -fdata-sections -funroll-loops - -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free + -fuse-ld=gold + ) set(LINKER_OPTIONS ${LINKER_OPTIONS} -fuse-ld=gold ) -if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") - set(COMPILE_FLAGS -stdlib=libc++ -Wmove ${COMPILE_FLAGS} -fwhole-program-vtables) - set(LINKER_OPTIONS ${LINKER_OPTIONS} -fwhole-program-vtables) - set(LINKER_FLAGS ${LINKER_FLAGS} c++) -elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") - set(COMPILE_FLAGS ${COMPILE_FLAGS} -fuse-ld=gold -fdevirtualize-at-ltrans) +set(LINKER_FLAGS stdc++) + +if (${CMAKE_BUILD_TYPE} STREQUAL "Release") + set(COMPILE_FLAGS + ${COMPILE_FLAGS} + -ffunction-sections + -fdata-sections + -fdevirtualize-at-ltrans + -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free + ) set(LINKER_OPTIONS ${LINKER_OPTIONS} -fdevirtualize-at-ltrans) - set(LINKER_FLAGS ${LINKER_FLAGS} stdc++) + set(LINKER_FLAGS ${LINKER_FLAGS} ${Jemalloc_LIBRARIES}) endif () @@ -51,9 +54,15 @@ set_target_properties(AdventOfCode2023 pb_1 PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON UNITY_BUILD ON ) + target_compile_definitions(AdventOfCode2023 PUBLIC $<$,$,GNU>>:_GLIBCXX_DEBUG>) target_compile_definitions(pb_1 PUBLIC $<$,$,GNU>>:_GLIBCXX_DEBUG>) -target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_OPTIONS}) + +target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_FLAGS}) +target_compile_options(pb_1 PUBLIC ${COMPILE_FLAGS}) + target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS}) -target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${Jemalloc_LIBRARIES}) -target_link_libraries(pb_1 PUBLIC ${Jemalloc_LIBRARIES}) +target_link_options(pb_1 PUBLIC ${LINKER_OPTIONS}) + +target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${LINKER_FLAGS}) +target_link_libraries(pb_1 PUBLIC ${LINKER_FLAGS}) diff --git a/inputs/pb1_p2_sample.txt b/inputs/pb1_p2_sample.txt index f90c606..0ec156b 100644 --- a/inputs/pb1_p2_sample.txt +++ b/inputs/pb1_p2_sample.txt @@ -1,6 +1,6 @@ -abcone2threexyz -two1nine eightwothree +two1ninethr +abcone2threexyz xtwone3four 4nineeightseven2 zoneight234 diff --git a/pb_1/problem_1.cpp b/pb_1/problem_1.cpp index dc15c67..1d19246 100644 --- a/pb_1/problem_1.cpp +++ b/pb_1/problem_1.cpp @@ -96,33 +96,45 @@ namespace pb1 { 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; - const std::uint32_t nb_characters_remaining = toSearch.size() - i - 1; + const std::uint32_t nb_characters_remaining = toSearch.size() - i; switch (toSearch[i]) { case 'o': { - if (nb_characters_remaining < digitLetters[0].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 (nb_characters_remaining >= digitLetters[0].size()) { + word.append(toSearch.substr(i, digitLetters[0].size())); + if (numbersSet.contains(word)) { + line_with_numbers.push_back(numbersSet.at(word)); } - } else { - line_with_numbers.push_back(numbersSet.at(word)); - ++i; } word.clear(); } break; case 't': { - if (nb_characters_remaining < digitLetters[2].size()) { + if (nb_characters_remaining >= digitLetters[1].size()) { + // TODO: problème si on a two mais là ça ajoute + de caractère… + const auto number_two = toSearch.substr(i, digitLetters[1].size()); + const auto number_three = [&]() -> std::string_view { + const auto tmp = toSearch.substr(i); + if (tmp.size() <= digitLetters[2].size()) { + return toSearch.substr(i); + } else { + return toSearch.substr(i, digitLetters[2].size()); + } + }(); + if (numbersSet.contains(number_two) or numbersSet.contains(number_three)) { + word.append(numbersSet.contains(number_two) ? number_two : number_three); + line_with_numbers.push_back(numbersSet.at(word)); + } + } + word.clear(); + } + break; + case 'f': { + // length five and four are equal + if (nb_characters_remaining < digitLetters[3].size()) { line_with_numbers.append(toSearch.substr(i)); continue; } - word.append(toSearch.substr(i, digitLetters[2].size())); + word.append(toSearch.substr(i, digitLetters[3].size())); if (!numbersSet.contains(word)) { const auto pos = word.find_first_of(digits); if (pos != std::string::npos) { @@ -130,24 +142,79 @@ namespace pb1 { } } else { line_with_numbers.push_back(numbersSet.at(word)); - i += 2; } word.clear(); } break; - case 'f': + case 's': { + if (nb_characters_remaining < digitLetters[6].size()) { + line_with_numbers.append(toSearch.substr(i)); + continue; + } + const auto number_six = toSearch.substr(i, digitLetters[5].size()); + const auto number_seven = toSearch.substr(i, digitLetters[6].size()); + if (!numbersSet.contains(number_six) and !numbersSet.contains(number_seven)) { + const auto pos = number_seven.find_first_of(digits); + if (pos != std::string::npos) { + line_with_numbers.push_back(word[pos]); + } + } else { + line_with_numbers.push_back(numbersSet.at(word)); + } + word.clear(); + } break; - case 's': + case 'e': { + if (nb_characters_remaining < digitLetters[7].size()) { + line_with_numbers.append(toSearch.substr(i)); + continue; + } + word.append(toSearch.substr(i, digitLetters[7].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]); + } + } else { + line_with_numbers.push_back(numbersSet.at(word)); + } + word.clear(); + } break; - case 'e': + case 'n': { + if (nb_characters_remaining < digitLetters.back().size()) { + line_with_numbers.append(toSearch.substr(i)); + continue; + } + word.append(toSearch.substr(i, digitLetters.back().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]); + } + } else { + line_with_numbers.push_back(numbersSet.at(word)); + } + word.clear(); + } break; - case 'n': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + line_with_numbers.push_back(toSearch[i]); break; default: if ((toSearch[i] - '0') < 10) { line_with_numbers.push_back(toSearch[i]); } - continue; + break; } } if (!line_with_numbers.empty()) {