aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/2022/day14/README.md35
-rw-r--r--src/2022/day14/aoc.cpp27
-rw-r--r--src/2022/day14/aoc.h23
-rw-r--r--test/test_2022.cpp5
4 files changed, 84 insertions, 6 deletions
diff --git a/src/2022/day14/README.md b/src/2022/day14/README.md
index 5a7d3e9..c2bd278 100644
--- a/src/2022/day14/README.md
+++ b/src/2022/day14/README.md
@@ -112,4 +112,37 @@ Once all 24 units of sand shown above have come to rest, all further sand flows
~..........
Using your scan, simulate the falling sand. How many units of sand come to rest before sand starts flowing into the abyss below?
-
+--- Part Two ---
+You realize you misread the scan. There isn't an endless void at the bottom of the scan - there's floor, and you're standing on it!
+
+You don't have time to scan the floor, so assume the floor is an infinite horizontal line with a y coordinate equal to two plus the highest y coordinate of any point in your scan.
+
+In the example above, the highest y coordinate of any point is 9, and so the floor is at y=11. (This is as if your scan contained one extra rock path like -infinity,11 -> infinity,11.) With the added floor, the example above now looks like this:
+
+ ...........+........
+ ....................
+ ....................
+ ....................
+ .........#...##.....
+ .........#...#......
+ .......###...#......
+ .............#......
+ .............#......
+ .....#########......
+ ....................
+<-- etc #################### etc -->
+To find somewhere safe to stand, you'll need to simulate falling sand until a unit of sand comes to rest at 500,0, blocking the source entirely and stopping the flow of sand into the cave. In the example above, the situation finally looks like this after 93 units of sand come to rest:
+
+............o............
+...........ooo...........
+..........ooooo..........
+.........ooooooo.........
+........oo#ooo##o........
+.......ooo#ooo#ooo.......
+......oo###ooo#oooo......
+.....oooo.oooo#ooooo.....
+....oooooooooo#oooooo....
+...ooo#########ooooooo...
+..ooooo.......ooooooooo..
+#########################
+Using your scan, simulate the falling sand until the source of the sand becomes blocked. How many units of sand come to rest?
diff --git a/src/2022/day14/aoc.cpp b/src/2022/day14/aoc.cpp
index a96877d..00bc0c8 100644
--- a/src/2022/day14/aoc.cpp
+++ b/src/2022/day14/aoc.cpp
@@ -3,6 +3,29 @@
namespace aoc2022 {
rock::three rock::t3;
+int fall1(cave& cv) {
+ bool fall{true};
+ int n{0};
+ while(fall) {
+ auto p = cv.drop(cv.to({500,0}));
+ rock::pos ns[] = {{p.x, p.y + 1}, {p.x - 1, p.y + 1}, {p.x + 1, p.y + 1}};
+ for (int i = 0; i < 3; i++) {
+ if (!cv.valid(ns[i])) {
+ fall = false;
+ }
+ }
+ if (fall) {
+ cv.get(p) = 'o';
+ n++;
+ }
+ }
+ return n;
+}
+
+int fall2(cave& cv) {
+ return 0;
+}
+
std::pair<int, int> day14(line_view file) {
std::vector<rock> rocks;
per_line(file, [&rocks](line_view lv){
@@ -17,8 +40,8 @@ std::pair<int, int> day14(line_view file) {
cv.mark(r);
}
- // cv.print();
- return {0, 0};
+ cave cv2{cv};
+ return {fall1(cv), fall2(cv2)};
}
}
diff --git a/src/2022/day14/aoc.h b/src/2022/day14/aoc.h
index 94598b9..d2143f6 100644
--- a/src/2022/day14/aoc.h
+++ b/src/2022/day14/aoc.h
@@ -63,13 +63,20 @@ struct cave {
int height = 0;
char* space;
- cave(int w, int h): width(w+1), height(h + 1) {
+ cave(int w, int h): width(w + 1), height(h + 1) {
space = (char *) malloc(width * height);
for (int i = 0; i < width * height; i++) {
*(space + i) = '.';
}
}
+ cave(cave& c) {
+ width = c.width;
+ height = c.height;
+ space = (char *) malloc(width * height);
+ memcpy(space, c.space, width * height);
+ }
+
rock::pos to(rock::pos p) {
return {p.x - rock::t3.minx, p.y};
}
@@ -78,6 +85,20 @@ struct cave {
return *(space + p.y * width + p.x);
}
+ bool valid(rock::pos p) {
+ return p.x >= 0 && p.x < width && p.y >= 0 && p.y < height;
+ }
+
+ rock::pos drop(rock::pos p) {
+ rock::pos ns[] = {{p.x, p.y + 1}, {p.x - 1, p.y + 1}, {p.x + 1, p.y + 1}};
+ for (int i = 0; i < 3; i++) {
+ if (valid(ns[i]) && get(ns[i]) == '.') {
+ return drop(ns[i]);
+ }
+ }
+ return p;
+ }
+
void mark(rock& r) {
for (size_t i = 0; i < r.rs.size() - 1 ; i++) {
auto r1 = r.rs[i];
diff --git a/test/test_2022.cpp b/test/test_2022.cpp
index cc97269..0ba594d 100644
--- a/test/test_2022.cpp
+++ b/test/test_2022.cpp
@@ -110,8 +110,9 @@ TEST_CASE("Distress Signal", "[2022]") {
}
TEST_CASE("Regolith Reservoir", "[2022]") {
- line_view lv = load_file("../src/2022/day14/input");
+ line_view lv = load_file("../src/2022/day14/input0");
auto p = aoc2022::day14(lv);
- REQUIRE(0 == p.first);
+ // REQUIRE(1133 == p.first);
+ REQUIRE(24 == p.first);
REQUIRE(0 == p.second);
}