diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-12-14 17:39:30 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-12-14 17:39:30 +0800 |
commit | 0b9455faebaf32549f0524a93d24bcadb06dfc24 (patch) | |
tree | 95c5da86830b3d9a54f38a84b41b324d24fb68fe /src/2022/day14/aoc.h | |
parent | 216aab22ad4fa72b37530d1e380bf3c5e179dc73 (diff) | |
download | advent-of-code-0b9455faebaf32549f0524a93d24bcadb06dfc24.tar.gz advent-of-code-0b9455faebaf32549f0524a93d24bcadb06dfc24.zip |
2022 day14 part1
Diffstat (limited to 'src/2022/day14/aoc.h')
-rw-r--r-- | src/2022/day14/aoc.h | 23 |
1 files changed, 22 insertions, 1 deletions
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]; |