use unsigned
This commit is contained in:
parent
fb82f11b39
commit
c18cb831ff
27
src/day7.cpp
27
src/day7.cpp
@ -11,8 +11,8 @@
|
|||||||
using Grid = std::vector<std::string>;
|
using Grid = std::vector<std::string>;
|
||||||
using Location = std::tuple<size_t, size_t>;
|
using Location = std::tuple<size_t, size_t>;
|
||||||
|
|
||||||
int64_t part_1(const Grid &grid);
|
uint64_t part_1(const Grid &grid);
|
||||||
int64_t part_2(const Grid &grid);
|
uint64_t part_2(const Grid &grid);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Grid grid;
|
Grid grid;
|
||||||
@ -38,15 +38,15 @@ Location find_start(const Grid &grid) {
|
|||||||
for (int64_t row{0}; row < grid.size(); ++row) {
|
for (int64_t row{0}; row < grid.size(); ++row) {
|
||||||
for (int64_t col{0}; col < grid[row].length(); ++col) {
|
for (int64_t col{0}; col < grid[row].length(); ++col) {
|
||||||
if (grid[row][col] == 'S') {
|
if (grid[row][col] == 'S') {
|
||||||
return Location{row, col};
|
return {row, col};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw "unreachable";
|
throw "unreachable";
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t part_1(const Grid &grid) {
|
uint64_t part_1(const Grid &grid) {
|
||||||
int64_t ans{0};
|
uint64_t ans{0};
|
||||||
Location start = find_start(grid);
|
Location start = find_start(grid);
|
||||||
const auto [start_row, start_col]{start};
|
const auto [start_row, start_col]{start};
|
||||||
std::set<size_t> cols{};
|
std::set<size_t> cols{};
|
||||||
@ -67,23 +67,24 @@ int64_t part_1(const Grid &grid) {
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert_or_increment(std::map<size_t, int64_t> &cols, size_t col,
|
void insert_or_increment(std::map<size_t, uint64_t> &cols, size_t col,
|
||||||
int64_t value) {
|
int64_t value) {
|
||||||
auto iter = cols.find(col);
|
auto iter = cols.find(col);
|
||||||
if (iter == cols.end()) {
|
if (iter == cols.end()) {
|
||||||
cols.insert({col, value});
|
cols.insert({col, value});
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
iter->second += value;
|
iter->second += value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t part_2(const Grid &grid) {
|
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};
|
||||||
std::map<size_t, int64_t> cols;
|
std::map<size_t, uint64_t> cols;
|
||||||
cols.insert(std::make_pair(start_col, 1));
|
cols.insert({start_col, 1});
|
||||||
for (size_t row{start_row}; row < grid.size(); ++row) {
|
for (size_t row{start_row}; row < grid.size(); ++row) {
|
||||||
std::map<size_t, int64_t> next_cols;
|
std::map<size_t, uint64_t> next_cols;
|
||||||
for (auto [col, cnt] : cols) {
|
for (auto [col, cnt] : cols) {
|
||||||
if (at_loc(grid, row, col) == '^') {
|
if (at_loc(grid, row, col) == '^') {
|
||||||
insert_or_increment(next_cols, col - 1, cnt);
|
insert_or_increment(next_cols, col - 1, cnt);
|
||||||
@ -95,6 +96,6 @@ int64_t part_2(const Grid &grid) {
|
|||||||
cols = std::move(next_cols);
|
cols = std::move(next_cols);
|
||||||
}
|
}
|
||||||
return std::accumulate(
|
return std::accumulate(
|
||||||
cols.begin(), cols.end(), 0ll,
|
cols.begin(), cols.end(), 0ull,
|
||||||
[](int64_t acc, auto entry) { return acc + entry.second; });
|
[](const uint64_t acc, const auto &entry) { return acc + entry.second; });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user