From ed1c1037af17172e1f6e8ae9242a515ae63d5a0e Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Thu, 3 Dec 2020 00:24:50 -0500 Subject: [PATCH] Use reduce in day3 --- day3/day3.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/day3/day3.cpp b/day3/day3.cpp index 246296a..a11fdba 100644 --- a/day3/day3.cpp +++ b/day3/day3.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include constexpr char TREE_CHAR = '#'; @@ -39,13 +41,20 @@ int part1(const std::vector &input) { } long part2(const std::vector &input) { - return 1L * findNumTrees(input, 1, 1) * findNumTrees(input, 3, 1) * findNumTrees(input, 5, 1) * - findNumTrees(input, 7, 1) * findNumTrees(input, 1, 2); + // deltas in the x and y directions respectively + std::vector> deltas{std::pair(1, 1), std::pair(3, 1), + std::pair(5, 1), std::pair(7, 1), + std::pair(1, 2)}; + + return std::reduce(std::execution::par, deltas.begin(), deltas.end(), 1L, + [&](long total, std::pair step_deltas) { + return total * findNumTrees(input, step_deltas.first, step_deltas.second); + }); } int main(int argc, char *argv[]) { if (argc != 2) { - std::cerr << "./" << argv[0] << "" << std::endl; + std::cerr << argv[0] << "" << std::endl; return 1; }