From 50d538760838094218642c30b72d402dff1d2ae5 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:23:08 +0300 Subject: day02: move lambdas out of loop --- day04/solution.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'day04/solution.cpp') diff --git a/day04/solution.cpp b/day04/solution.cpp index 6fa5ccd..fe33120 100644 --- a/day04/solution.cpp +++ b/day04/solution.cpp @@ -51,27 +51,27 @@ void part1(auto input) { views::zip(decr, decr), // top-left }; - for (const auto [i, j] : + auto in_bounds = [width, height](IndexPair e) -> bool { + auto &&[i, j] = e; + return 0 <= i and i < height and 0 <= j and j < width; + }; + + auto get_input_element = [input](IndexPair e) -> char { + auto &&[i, j] = e; + return input[i][j]; + }; + + for (auto &&[i, j] : views::cartesian_product(views::iota(0, width), views::iota(0, height))) { + auto add_offset = [i, j](IndexPair e) { + const auto [di, dj] = e; + return IndexPair{i + di, j + dj}; + }; + // std::apply is needed to unfold the ‘directions’ tuple. std::apply([=, &answer](auto &&... dirs) { - auto add_offset = [i, j](IndexPair e) { - const auto [di, dj] = e; - return std::tuple{i + di, j + dj}; - }; - - auto in_bounds = [width, height](IndexPair e) -> bool { - const auto [i, j] = e; - return 0 <= i and i < height and 0 <= j and j < width; - }; - - auto get_input_element = [input](IndexPair e) -> char { - const auto [i, j] = e; - return input[i][j]; - }; - (((answer += ranges::equal(dirs | views::transform(add_offset) | views::filter(in_bounds) -- cgit v1.2.3