they see me foldin'
This commit is contained in:
parent
8ff89aa2c2
commit
f16918d137
35
src/day7.cpp
35
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<size_t, uint64_t> starting_cols{{start_col, 1}};
|
||||
auto cols = std::accumulate(view.begin(), view.end(), starting_cols, [&grid](auto acc, auto row){
|
||||
std::map<size_t, uint64_t> 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<size_t, uint64_t>{},
|
||||
[&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;
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user