From f16918d137ce3e020e11838f960733502dbf7263 Mon Sep 17 00:00:00 2001 From: ajet Date: Sun, 28 Dec 2025 05:39:48 -1000 Subject: [PATCH] they see me foldin' --- src/day7.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/day7.cpp b/src/day7.cpp index 83f366c..2945334 100644 --- a/src/day7.cpp +++ b/src/day7.cpp @@ -77,21 +77,24 @@ uint64_t part_2(const Grid &grid) { Location start = find_start(grid); const auto [start_row, start_col]{start}; using namespace std::ranges::views; - auto view = std::ranges::iota_view{start_row, grid.size()}; + + using namespace std::ranges; + auto view = iota_view{start_row, grid.size()}; std::map starting_cols{{start_col, 1}}; - auto cols = std::accumulate(view.begin(), view.end(), starting_cols, [&grid](auto acc, auto row){ - std::map next_cols; - for (auto [col, cnt] : acc) { - if (at_loc(grid, row, col) == '^') { - insert_or_increment(next_cols, col - 1, cnt); - insert_or_increment(next_cols, col + 1, cnt); - } else { - insert_or_increment(next_cols, col, cnt); - } - } - return next_cols; - }); - return std::accumulate( - cols.begin(), cols.end(), 0ull, - [](const uint64_t acc, const auto &entry) { return acc + entry.second; }); + auto cols = fold_left(view, starting_cols, [&grid](auto acc, auto row) { + return fold_left(acc, std::map{}, + [&grid, &row](auto next_cols, auto col_cnt) { + auto [col, cnt] = col_cnt; + if (at_loc(grid, row, col) == '^') { + insert_or_increment(next_cols, col - 1, cnt); + insert_or_increment(next_cols, col + 1, cnt); + } else { + insert_or_increment(next_cols, col, cnt); + } + return next_cols; + }); + }); + return fold_left(cols, 0ull, [](const uint64_t acc, const auto &entry) { + return acc + entry.second; + }); }