Skip to content

Commit

Permalink
2023 day 14: skip Grid bounds checking
Browse files Browse the repository at this point in the history
C++23 multidimensional subscripting would be nice here.
  • Loading branch information
yut23 committed Mar 22, 2024
1 parent 6e1b562 commit c06933a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions 2023/src/day14.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void Platform::transpose() {
const int n = rocks.width;
for (int y = 0; y < n; ++y) {
for (int x = 0; x < y; ++x) {
std::swap(rocks.at(x, y), rocks.at(y, x));
std::swap(rocks.at_unchecked(x, y), rocks.at_unchecked(y, x));
}
}
}
Expand All @@ -58,7 +58,7 @@ void Platform::flip_rows() {
for (int y = 0; y < n; ++y) {
for (int x = 0; x < n / 2; ++x) {
const int z = n - x - 1;
std::swap(rocks.at(x, y), rocks.at(z, y));
std::swap(rocks.at_unchecked(x, y), rocks.at_unchecked(z, y));
}
}
}
Expand All @@ -68,7 +68,7 @@ void Platform::flip_columns() {
for (int x = 0; x < n; ++x) {
for (int y = 0; y < n / 2; ++y) {
const int z = n - y - 1;
std::swap(rocks.at(x, y), rocks.at(x, z));
std::swap(rocks.at_unchecked(x, y), rocks.at_unchecked(x, z));
}
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ int Platform::calculate_load() const {
int load = 0;
for (int y = 0; y < rocks.height; ++y) {
for (int x = 0; x < rocks.width; ++x) {
if (rocks.at(x, y) == Rock::round) {
if (rocks.at_unchecked(x, y) == Rock::round) {
load += rocks.width - x;
}
}
Expand Down
6 changes: 6 additions & 0 deletions 2023/src/ds/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ struct Grid {
constexpr const_reference at(size_type x, size_type y) const {
return m_data.at(get_index(x, y));
}
constexpr reference at_unchecked(size_type x, size_type y) {
return m_data[get_index(x, y)];
}
constexpr const_reference at_unchecked(size_type x, size_type y) const {
return m_data[get_index(x, y)];
}

// indexing by Pos
constexpr reference at(const Pos &pos) { return at(pos.x, pos.y); }
Expand Down

0 comments on commit c06933a

Please sign in to comment.