From d7bf213e7bde1bbbd0b12cc8ebd5bff6967f9c90 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 6 Dec 2023 22:29:07 +0100 Subject: [PATCH] Finally working for one case. --- CMakeLists.txt | 7 +++-- inputs/pb1_p2_sample.txt | 2 +- pb_1/problem_1.cpp | 62 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0c44b1..feb914d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,7 @@ find_package(PkgConfig REQUIRED) pkg_check_modules(Jemalloc REQUIRED jemalloc) -set(LINKER_OPTIONS - PRIVATE -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 @@ -52,7 +51,9 @@ 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_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS}) target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${Jemalloc_LIBRARIES}) -target_link_libraries(pb_1 ${Jemalloc_LIBRARIES}) +target_link_libraries(pb_1 PUBLIC ${Jemalloc_LIBRARIES}) diff --git a/inputs/pb1_p2_sample.txt b/inputs/pb1_p2_sample.txt index 4316a6b..f90c606 100644 --- a/inputs/pb1_p2_sample.txt +++ b/inputs/pb1_p2_sample.txt @@ -1,6 +1,6 @@ +abcone2threexyz two1nine eightwothree -abcone2threexyz xtwone3four 4nineeightseven2 zoneight234 diff --git a/pb_1/problem_1.cpp b/pb_1/problem_1.cpp index 768617b..04bd85d 100644 --- a/pb_1/problem_1.cpp +++ b/pb_1/problem_1.cpp @@ -4,6 +4,10 @@ #include "problem_1.hpp" #include +#include +#include +#include +#include #include #include #include @@ -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 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 &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'); } }