From a04b983fe09e026792c448283a333f4d777ed234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20L=C3=A4ufer?= Date: Wed, 1 Nov 2023 19:29:33 -0500 Subject: [PATCH] split range v3 example into main and test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Konstantin Läufer --- modern-cpp/CMakeLists.txt | 4 +-- modern-cpp/rangev3-aoc2022day1.cpp | 26 ++----------------- modern-cpp/rangev3-aoc2022day1_main.cpp | 34 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 modern-cpp/rangev3-aoc2022day1_main.cpp diff --git a/modern-cpp/CMakeLists.txt b/modern-cpp/CMakeLists.txt index 6436103..c290ff7 100644 --- a/modern-cpp/CMakeLists.txt +++ b/modern-cpp/CMakeLists.txt @@ -28,8 +28,8 @@ foreach(APP_NAME IN LISTS MY_APP_NAMES) add_my_executable(${APP_NAME}) endforeach() -# add_executable(rangev3-aoc2022day1 rangev3-aoc2022day1.cpp) -# target_link_libraries(rangev3-aoc2022day1 range-v3 gtest_main) +add_executable(rangev3-aoc2022day1_main rangev3-aoc2022day1_main.cpp rangev3-aoc2022day1.cpp) +target_link_libraries(rangev3-aoc2022day1_main range-v3) enable_testing() diff --git a/modern-cpp/rangev3-aoc2022day1.cpp b/modern-cpp/rangev3-aoc2022day1.cpp index a852275..e149680 100644 --- a/modern-cpp/rangev3-aoc2022day1.cpp +++ b/modern-cpp/rangev3-aoc2022day1.cpp @@ -1,7 +1,5 @@ #include "rangev3-aoc2022day1.hpp" -#include -#include #include #include #include @@ -12,22 +10,6 @@ #include #include -#include - -// std::pair most_nutritious_inventories(std::string_view const filename) { -// using namespace ranges; - -// auto line_to_int = [](auto const& s) { return s.empty() ? 0 : std::stoi(s); }; - -// // read lines into vector to support chunk_by, which requires a forward range -// std::ifstream input{filename.data()}; -// std::vector data; -// copy( -// getlines(input) | views::transform(line_to_int), -// back_inserter(data) -// ); -// } - using namespace ranges; std::pair most_nutritious_inventories(std::vector data) { @@ -46,6 +28,8 @@ std::pair most_nutritious_inventories(std::vector data) { make_heap(inventories); auto constexpr n = 3; + if (inventories.size() < n) return std::make_pair(-1, -1); + std::vector top_n; // retrieve top n inventory values @@ -58,9 +42,3 @@ std::pair most_nutritious_inventories(std::vector data) { // return top and sum of top n inventory values return std::make_pair(front(top_n), accumulate(top_n, 0)); } - -// TEST(AOC2022Day1, RealData) { -// auto const result = most_nutritious_inventories("data/day1input.txt"); -// std::cout << "Day 1 part 1 (max) = " << result.first << "\n"; -// std::cout << "Day 1 part 2 (sum) = " << result.second << "\n"; -// } diff --git a/modern-cpp/rangev3-aoc2022day1_main.cpp b/modern-cpp/rangev3-aoc2022day1_main.cpp new file mode 100644 index 0000000..1265d98 --- /dev/null +++ b/modern-cpp/rangev3-aoc2022day1_main.cpp @@ -0,0 +1,34 @@ +#include "rangev3-aoc2022day1.hpp" + +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace ranges; + +// to use, redirect stdin from data/day1input.txt +int main() { + // read lines into vector + std::vector data; + copy( + getlines(std::cin) + | views::transform( + [](auto const& s) { return s.empty() ? 0 : std::stoi(s); } + ), + back_inserter(data) + ); + + copy(data, std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + auto result = most_nutritious_inventories(data); + + std::cout << "Day 1 part 1 (max) = " << result.first << "\n"; + std::cout << "Day 1 part 2 (sum) = " << result.second << "\n"; +}