they see me foldin'

This commit is contained in:
Adam Jeniski 2025-12-28 05:39:48 -10:00
parent 8ff89aa2c2
commit f16918d137

View File

@ -77,21 +77,24 @@ uint64_t part_2(const Grid &grid) {
Location start = find_start(grid); Location start = find_start(grid);
const auto [start_row, start_col]{start}; const auto [start_row, start_col]{start};
using namespace std::ranges::views; 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<size_t, uint64_t> starting_cols{{start_col, 1}}; std::map<size_t, uint64_t> starting_cols{{start_col, 1}};
auto cols = std::accumulate(view.begin(), view.end(), starting_cols, [&grid](auto acc, auto row){ auto cols = fold_left(view, starting_cols, [&grid](auto acc, auto row) {
std::map<size_t, uint64_t> next_cols; return fold_left(acc, std::map<size_t, uint64_t>{},
for (auto [col, cnt] : acc) { [&grid, &row](auto next_cols, auto col_cnt) {
if (at_loc(grid, row, col) == '^') { auto [col, cnt] = col_cnt;
insert_or_increment(next_cols, col - 1, 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 + 1, cnt);
insert_or_increment(next_cols, col, cnt); } else {
} insert_or_increment(next_cols, col, cnt);
} }
return next_cols; return next_cols;
}); });
return std::accumulate( });
cols.begin(), cols.end(), 0ull, return fold_left(cols, 0ull, [](const uint64_t acc, const auto &entry) {
[](const uint64_t acc, const auto &entry) { return acc + entry.second; }); return acc + entry.second;
});
} }