Skip to content

Commit

Permalink
split range v3 example into main and test
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Nov 2, 2023
1 parent 91e0b6f commit a04b983
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions modern-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
26 changes: 2 additions & 24 deletions modern-cpp/rangev3-aoc2022day1.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "rangev3-aoc2022day1.hpp"

#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -12,22 +10,6 @@
#include <range/v3/numeric.hpp>
#include <range/v3/action.hpp>

#include <gtest/gtest.h>

// std::pair<int, int> 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<int> data;
// copy(
// getlines(input) | views::transform(line_to_int),
// back_inserter(data)
// );
// }

using namespace ranges;

std::pair<int, int> most_nutritious_inventories(std::vector<int> data) {
Expand All @@ -46,6 +28,8 @@ std::pair<int, int> most_nutritious_inventories(std::vector<int> data) {
make_heap(inventories);

auto constexpr n = 3;
if (inventories.size() < n) return std::make_pair(-1, -1);

std::vector<int> top_n;

// retrieve top n inventory values
Expand All @@ -58,9 +42,3 @@ std::pair<int, int> most_nutritious_inventories(std::vector<int> 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";
// }
34 changes: 34 additions & 0 deletions modern-cpp/rangev3-aoc2022day1_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "rangev3-aoc2022day1.hpp"

#include <iostream>
#include <utility>
#include <vector>

#include <range/v3/view.hpp>
#include <range/v3/algorithm.hpp>
#include <range/v3/iterator.hpp>
#include <range/v3/numeric.hpp>
#include <range/v3/action.hpp>

using namespace ranges;

// to use, redirect stdin from data/day1input.txt
int main() {
// read lines into vector
std::vector<int> 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<int>(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";
}

0 comments on commit a04b983

Please sign in to comment.