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