aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day14/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-14 22:32:20 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-14 22:32:20 +0800
commit363e289eb2b54757e52dc93efa3090c763a8aa39 (patch)
treec0965fb434472c3543beba5cdd976c03da86611f /src/2022/day14/aoc.cpp
parentc277bc6e370bc5ee8899055a112978c76c1e7b19 (diff)
downloadadvent-of-code-363e289eb2b54757e52dc93efa3090c763a8aa39.tar.gz
advent-of-code-363e289eb2b54757e52dc93efa3090c763a8aa39.zip
2022 day14
Diffstat (limited to 'src/2022/day14/aoc.cpp')
-rw-r--r--src/2022/day14/aoc.cpp40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/2022/day14/aoc.cpp b/src/2022/day14/aoc.cpp
index 00bc0c8..614c432 100644
--- a/src/2022/day14/aoc.cpp
+++ b/src/2022/day14/aoc.cpp
@@ -3,11 +3,11 @@
namespace aoc2022 {
rock::three rock::t3;
-int fall1(cave& cv) {
+int fall1(cave& cv, int dx) {
bool fall{true};
int n{0};
- while(fall) {
- auto p = cv.drop(cv.to({500,0}));
+ while (fall) {
+ auto p = cv.drop(cv.to({500,0}, dx));
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])) {
@@ -22,8 +22,20 @@ int fall1(cave& cv) {
return n;
}
-int fall2(cave& cv) {
- return 0;
+int fall2(cave& cv, int dx) {
+ bool fall{true};
+ int n{0};
+ while (fall) {
+ auto p0 = cv.to({500,0}, dx);
+ auto p = cv.drop(p0);
+
+ cv.get(p) = 'o';
+ n++;
+ if (p.x == p0.x && p.y == p0.y) {
+ fall = false;
+ }
+ }
+ return n;
}
std::pair<int, int> day14(line_view file) {
@@ -34,14 +46,24 @@ std::pair<int, int> day14(line_view file) {
});
// printf("%d %d %d\n", rock::t3.minx, rock::t3.maxx, rock::t3.maxy);
- cave cv(rock::t3.maxx - rock::t3.minx, rock::t3.maxy);
+ cave cv1(rock::t3.maxx - rock::t3.minx, rock::t3.maxy);
+ cave cv2(2 * (rock::t3.maxy + 2), rock::t3.maxy + 2);
+ int dx = (rock::t3.maxy + 2) - (500 - rock::t3.minx);
for(auto& r: rocks) {
// r.print();
- cv.mark(r);
+ cv1.mark(r);
+ cv2.mark(r, dx);
+ }
+ auto n1 = fall1(cv1, 0);
+
+ for(int i = 0; i < cv2.width; i++) {
+ cv2.get({i, cv2.height - 1}) = '#';
}
- cave cv2{cv};
- return {fall1(cv), fall2(cv2)};
+ auto n2 = fall2(cv2, dx);
+ // cv2.print();
+
+ return {n1, n2};
}
}