aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-17 08:55:03 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-17 08:55:03 +0800
commit2e87804114b6c1e732ce002dc9a5f74db43e4cee (patch)
treece2be15532c317c166ad8bf1489d98099a2a182b
parent4747911566e69b1d95e88bb5955ffbeddfae6a08 (diff)
downloadadvent-of-code-2e87804114b6c1e732ce002dc9a5f74db43e4cee.tar.gz
advent-of-code-2e87804114b6c1e732ce002dc9a5f74db43e4cee.zip
grid
-rw-r--r--src/2015/day6/aoc.h15
-rw-r--r--test/test_2015.cpp2
2 files changed, 8 insertions, 9 deletions
diff --git a/src/2015/day6/aoc.h b/src/2015/day6/aoc.h
index b68ef18..96fecca 100644
--- a/src/2015/day6/aoc.h
+++ b/src/2015/day6/aoc.h
@@ -6,8 +6,7 @@ namespace aoc2015 {
template <size_t N>
struct grid {
- static_assert(N > 0 && ((N & (N - 1)) == 0), "N must be power of 2");
- constexpr static size_t size = N * N / 64;
+ constexpr static size_t size = N * N / 64 + 1;
uint64_t pool[size] = {0};
@@ -17,29 +16,29 @@ struct grid {
};
void set(int x, int y) {
- uint64_t p = x * N + y;
+ uint64_t p = y * N + x;
uint64_t r = p >> 6;
uint64_t c = p & 63;
uint64_t& byte = pool[r];
- uint64_t mask = 1 << c;
+ uint64_t mask = 1ll << c;
byte |= mask;
}
void reset(int x, int y) {
- uint64_t p = x * N + y;
+ uint64_t p = y * N + x;
uint64_t r = p >> 6;
uint64_t c = p & 63;
uint64_t& byte = pool[r];
- uint64_t mask = ~(1 << c);
+ uint64_t mask = ~(1ll << c);
byte &= mask;
}
void toggle(int x, int y) {
- uint64_t p = x * N + y;
+ uint64_t p = y * N + x;
uint64_t r = p >> 6;
uint64_t c = p & 63;
uint64_t& byte = pool[r];
- uint64_t mask = 1 << c;
+ uint64_t mask = 1ll << c;
byte ^= mask;
}
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index a6bb135..f1d643b 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -74,7 +74,7 @@ TEST_CASE("Doesn't He Have Intern-Elves For This?", "[day5]") {
}
TEST_CASE("Probably a Fire Hazard", "[day6]") {
- aoc2015::grid<1024> grid;
+ aoc2015::grid<1000> grid;
grid.turn_on({0,0}, {0,999});
REQUIRE(grid.count() == 1000);
}