diff options
author | dzwdz | 2022-12-18 11:46:34 +0100 |
---|---|---|
committer | dzwdz | 2022-12-18 11:46:34 +0100 |
commit | 566eb99fee1450feec1482680cd37383f3040724 (patch) | |
tree | 2ffdb67c474b990ef7183d598a31e238c43288df /22.18/main.cpp | |
parent | ccc7912daa19de575f2f248520a3ca947281c97e (diff) |
day 18 part 1
Diffstat (limited to '22.18/main.cpp')
-rw-r--r-- | 22.18/main.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/22.18/main.cpp b/22.18/main.cpp new file mode 100644 index 0000000..f4fbcea --- /dev/null +++ b/22.18/main.cpp @@ -0,0 +1,83 @@ +#include <cstdio> +#include <string> +#include <unordered_set> +using namespace std; + +struct Vec3 +{ + int x, y, z; + + bool operator==(const Vec3& o) const { + return o.x == x && o.y == y && o.z == z; + } + + int& operator[](int idx) { + switch (idx) { + case 0: return x; + case 1: return y; + case 2: return z; + default: + throw "whatever"; + } + } + + Vec3 operator+(const Vec3& o) const { + return { + x + o.x, + y + o.y, + z + o.z, + }; + } +}; + +/* from nullprogram */ +uint32_t +lowbias32(uint32_t x) +{ + x ^= x >> 16; + x *= 0x7feb352dU; + x ^= x >> 15; + x *= 0x846ca68bU; + x ^= x >> 16; + return x; +} + +template<> +struct std::hash<Vec3> +{ + size_t operator()(Vec3 const& v) const noexcept { + int h = 0; + h = lowbias32(h ^ v.x); + h = lowbias32(h ^ v.y); + h = lowbias32(h ^ v.z); + return h; + } +}; + +int +partOne(unordered_set<Vec3> const& map) +{ + int total = 0; + for (auto &v : map) { + for (int axis = 0; axis < 3; axis++) { + for (auto ad : {-1, 1}) { + Vec3 vd = v; + vd[axis] += ad; + if (map.count(vd) == 0) total++; + } + } + } + return total; +} + +int +main() +{ + int x, y, z; + unordered_set<Vec3> map; + while (scanf("%d,%d,%d ", &x, &y, &z) == 3) { + Vec3 v = {x, y, z}; + map.insert(v); + } + printf("%d\n", partOne(map)); +} |