Skip to content

Commit

Permalink
range v3 example - made number of top inventories a parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <laufer@cs.luc.edu>
  • Loading branch information
klaeufer committed Nov 2, 2023
1 parent a04b983 commit d6ad537
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 5 deletions.
3 changes: 1 addition & 2 deletions modern-cpp/rangev3-aoc2022day1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using namespace ranges;

std::pair<int, int> most_nutritious_inventories(std::vector<int> data) {
std::pair<int, int> most_nutritious_inventories(std::vector<int> data, const int n) {
auto has_no_zeroes = [](auto const& xs) {
return none_of(xs, [](auto const x) { return x == 0; });
};
Expand All @@ -27,7 +27,6 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion modern-cpp/rangev3-aoc2022day1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include <utility>
#include <vector>

std::pair<int, int> most_nutritious_inventories(std::vector<int> data);
std::pair<int, int> most_nutritious_inventories(std::vector<int> data, int n);

#endif // RANGEV3_AOC2022DAY1_H
2 changes: 1 addition & 1 deletion modern-cpp/rangev3-aoc2022day1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
copy(data, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;

auto result = most_nutritious_inventories(data);
auto result = most_nutritious_inventories(data, 3);

std::cout << "Day 1 part 1 (max) = " << result.first << "\n";
std::cout << "Day 1 part 2 (sum) = " << result.second << "\n";
Expand Down
2 changes: 1 addition & 1 deletion modern-cpp/rangev3-aoc2022day1_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ TEST(AOC2022Day1, Example) {
};
std::pair result{24000, 45000};

EXPECT_EQ(most_nutritious_inventories(data), result);
EXPECT_EQ(most_nutritious_inventories(data, 3), result);
}

0 comments on commit d6ad537

Please sign in to comment.