aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/2022/day18/README.md9
-rw-r--r--src/2022/day18/aoc.cpp49
-rw-r--r--src/2022/day18/aoc.h66
3 files changed, 121 insertions, 3 deletions
diff --git a/src/2022/day18/README.md b/src/2022/day18/README.md
index c5e4809..50be3fe 100644
--- a/src/2022/day18/README.md
+++ b/src/2022/day18/README.md
@@ -27,3 +27,12 @@ Here's a larger example:
In the above example, after counting up all the sides that aren't connected to another cube, the total surface area is 64.
What is the surface area of your scanned lava droplet?
+
+--- Part Two ---
+Something seems off about your calculation. The cooling rate depends on exterior surface area, but your calculation also included the surface area of air pockets trapped in the lava droplet.
+
+Instead, consider only cube sides that could be reached by the water and steam as the lava droplet tumbles into the pond. The steam will expand to reach as much as possible, completely displacing any air on the outside of the lava droplet but never expanding diagonally.
+
+In the larger example above, exactly one cube of air is trapped within the lava droplet (at 2,2,5), so the exterior surface area of the lava droplet is 58.
+
+What is the exterior surface area of your scanned lava droplet?
diff --git a/src/2022/day18/aoc.cpp b/src/2022/day18/aoc.cpp
index 56681a2..42cfe36 100644
--- a/src/2022/day18/aoc.cpp
+++ b/src/2022/day18/aoc.cpp
@@ -1,9 +1,54 @@
#include "aoc.h"
+#include <deque>
namespace aoc2022 {
-std::pair<int, int> day18(line_view) {
- return {0, 0};
+static int maxx = 0;
+static int maxy = 0;
+static int maxz = 0;
+
+bool is_valid(droplet d, cube& c) {
+ bool bx = d.x >= 0 && d.x < maxx + 1;
+ bool by = d.y >= 0 && d.y < maxy + 1;
+ bool bz = d.z >= 0 && d.z < maxz + 1;
+ return bx && by && bz && c.get(d.x, d.y, d.z) == 0;
}
+
+// flood
+void flood(cube& c) {
}
+std::pair<int, int> day18(line_view file) {
+ std::vector<droplet> ds;
+ per_line(file, [&ds](line_view lv) {
+ droplet d{lv};
+ maxx = std::max(maxx, d.x);
+ maxy = std::max(maxy, d.y);
+ maxz = std::max(maxz, d.z);
+
+ ds.emplace_back(d);
+ return true;
+ });
+
+ cube c{maxx + 1, maxy + 1, maxz + 1};
+ for (auto& d : ds) {
+ c.get(d.x, d.y, d.z) = 1;
+ }
+
+ flood(c);
+ int t0{0}, t1{0};
+ c.traverse([&t0, &c](int x, int y, int z) {
+ if (c.get(x, y, z) == 1) {
+ t0 += 6;
+ t0 -= c.get(x - 1, y, z);
+ t0 -= c.get(x + 1, y, z);
+ t0 -= c.get(x, y - 1, z);
+ t0 -= c.get(x, y + 1, z);
+ t0 -= c.get(x, y, z - 1);
+ t0 -= c.get(x, y, z + 1);
+ }
+ });
+
+ return {t0, t1};
+}
+} // namespace aoc2022
diff --git a/src/2022/day18/aoc.h b/src/2022/day18/aoc.h
index 55c9555..1d87b3c 100644
--- a/src/2022/day18/aoc.h
+++ b/src/2022/day18/aoc.h
@@ -3,5 +3,69 @@
namespace aoc2022 {
+struct cube {
+ int dx;
+ int dy;
+ int dz;
+
+ char* ds = nullptr;
+
+ char& get(int x, int y, int z) {
+ static char none{0};
+ bool bx = x >= 0 && x < dx;
+ bool by = y >= 0 && y < dy;
+ bool bz = z >= 0 && z < dz;
+ return (bx && by && bz) ? *(ds + z * (dx * dy) + y * dx + x) : none;
+ }
+
+ cube(int x, int y, int z) : dx(x), dy(y), dz(z) {
+ ds = (char*)malloc(x * y * z);
+ memset(ds, 0, x * y * z);
+ }
+
+ template <typename F, typename... Args>
+ void traverse(F&& f, Args&&... args) {
+ for (int z = 0; z < dz; z++) {
+ for (int y = 0; y < dy; y++) {
+ for (int x = 0; x < dx; x++) {
+ f(x, y, z, std::forward<Args>(args)...);
+ }
+ }
+ }
+ }
+};
+
+struct droplet {
+ int x = 0;
+ int y = 0;
+ int z = 0;
+
+ void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ }
+
+ void print() const noexcept { printf("%d,%d,%d\n", x, y, z); }
+
+ friend bool operator<(droplet d1, droplet d2) {
+ return d1.z < d2.z ? true : d1.z > d2.z ? false : d1.y < d2.y ? true : d1.y > d2.y ? false : d1.x < d2.x;
+ }
+
+ droplet(int xx, int yy, int zz) : x(xx), y(yy), z(zz) {}
+ droplet(line_view lv) {
+ const char* p = lv.line;
+ int* ds[] = {&x, &y, &z};
+ int i{0};
+ while (p < lv.line + lv.length) {
+ get_number(&p, ds[i++]);
+ p++;
+ }
+ }
+};
+
std::pair<int, int> day18(line_view);
-}
+} // namespace aoc2022