diff --git a/src/day7.cpp b/src/day7.cpp index 8c5cb07..3f8a6fe 100644 --- a/src/day7.cpp +++ b/src/day7.cpp @@ -11,8 +11,8 @@ using Grid = std::vector; using Location = std::tuple; -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 cols{}; @@ -67,23 +67,24 @@ int64_t part_1(const Grid &grid) { return ans; } -void insert_or_increment(std::map &cols, size_t col, - int64_t value) { +void insert_or_increment(std::map &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 cols; - cols.insert(std::make_pair(start_col, 1)); + std::map cols; + cols.insert({start_col, 1}); for (size_t row{start_row}; row < grid.size(); ++row) { - std::map next_cols; + std::map 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; }); }