diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-12-14 16:45:52 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-12-14 16:45:52 +0800 |
commit | 216aab22ad4fa72b37530d1e380bf3c5e179dc73 (patch) | |
tree | eaf03e6c94111e1d980fd39eaee898340f6a2a4b /src | |
parent | 6b36d30a067fbd01ed5f68c0791abfb2259f8868 (diff) | |
download | advent-of-code-216aab22ad4fa72b37530d1e380bf3c5e179dc73.tar.gz advent-of-code-216aab22ad4fa72b37530d1e380bf3c5e179dc73.zip |
2022 day12 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day14/aoc.cpp | 18 | ||||
-rw-r--r-- | src/2022/day14/aoc.h | 109 |
2 files changed, 126 insertions, 1 deletions
diff --git a/src/2022/day14/aoc.cpp b/src/2022/day14/aoc.cpp index a60f716..a96877d 100644 --- a/src/2022/day14/aoc.cpp +++ b/src/2022/day14/aoc.cpp @@ -1,7 +1,23 @@ #include "aoc.h" namespace aoc2022 { -std::pair<int, int> day14(line_view) { +rock::three rock::t3; + +std::pair<int, int> day14(line_view file) { + std::vector<rock> rocks; + per_line(file, [&rocks](line_view lv){ + rocks.emplace_back(lv); + return true; + }); + + // 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); + for(auto& r: rocks) { + // r.print(); + cv.mark(r); + } + + // cv.print(); return {0, 0}; } } diff --git a/src/2022/day14/aoc.h b/src/2022/day14/aoc.h index 799651d..94598b9 100644 --- a/src/2022/day14/aoc.h +++ b/src/2022/day14/aoc.h @@ -2,6 +2,115 @@ #include <vector> namespace aoc2022 { + +struct rock { + struct three { + int minx = INT32_MAX; + int maxx = INT32_MIN; + int maxy = INT32_MIN; + }; + + struct pos { + int x = 0; + int y = 0; + + pos(int xx, int yy): x(xx), y(yy) {} + }; + static three t3; + std::vector<pos> rs; + + void get_number(const char** p, int ds[], int* n) { + const char* p0 = *p; + while (true) { + while (*p0 >= '0' && *p0 <= '9') {{ + ds[*n] = 10 * ds[*n] + *p0 - '0'; + p0++; + }} + *n += 1; + if (*p0 == '\n') break; + else while(!(*p0 >= '0' && *p0 <= '9')) p0++; + } + *p = p0; + } + + rock (line_view lv) { + int is[100] = {0}; + int n{0}; + const char* p = lv.line; + get_number(&p, is, &n); + for(int i = 0; i < n; i++) { + if (i % 2 == 1) { + int x = is[i-1]; + int y = is[i]; + rs.push_back({x, y}); + if (t3.minx > x) t3.minx = x; + if (t3.maxx < x) t3.maxx = x; + if (t3.maxy < y) t3.maxy = y; + } + } + } + + void print() { + for (auto& p: rs) { + printf("(%d,%d) ", p.x, p.y); + } + printf("\n"); + } +}; + +struct cave { + int width = 0; + int height = 0; + + char* space; + 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) = '.'; + } + } + + rock::pos to(rock::pos p) { + return {p.x - rock::t3.minx, p.y}; + } + + char& get(rock::pos p) const noexcept { + return *(space + p.y * width + p.x); + } + + void mark(rock& r) { + for (size_t i = 0; i < r.rs.size() - 1 ; i++) { + auto r1 = r.rs[i]; + auto r2 = r.rs[i+1]; + if (r1.x == r2.x) { + int min = r1.y < r2.y ? r1.y : r2.y; + int max = r1.y > r2.y ? r1.y : r2.y; + for (int j = min; j <= max; j++) { + auto p = to({r1.x, j}); + get(p) = '#'; + } + } + if (r1.y == r2.y) { + int min = r1.x < r2.x ? r1.x : r2.x; + int max = r1.x > r2.x ? r1.x : r2.x; + for (int j = min; j <= max; j++) { + auto p = to({j, r1.y}); + get(p) = '#'; + } + } + } + } + + void print() const noexcept { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + printf("%c", get({x, y})); + } + printf("\n"); + } + } +}; + std::pair<int, int> day14(line_view); } |