diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2021/day4/aoc.cpp | 2 | ||||
-rw-r--r-- | src/2021/day5/README.md | 21 | ||||
-rw-r--r-- | src/2021/day5/aoc.cpp | 93 | ||||
-rw-r--r-- | src/2021/day5/aoc.h | 12 | ||||
-rw-r--r-- | src/2021/day5/input0 | 10 |
5 files changed, 136 insertions, 2 deletions
diff --git a/src/2021/day4/aoc.cpp b/src/2021/day4/aoc.cpp index 6212dcd..e8dbff8 100644 --- a/src/2021/day4/aoc.cpp +++ b/src/2021/day4/aoc.cpp @@ -12,7 +12,7 @@ struct bnum { bool operator<(const bnum& b) const noexcept { return v < b.v; } }; -void get_number(const char** pp, int* d) { +static void get_number(const char** pp, int* d) { const char* p = *pp; while (*p >= '0' && *p <= '9') { *d = *d * 10 + *p - '0'; diff --git a/src/2021/day5/README.md b/src/2021/day5/README.md index 445b7c2..948263c 100644 --- a/src/2021/day5/README.md +++ b/src/2021/day5/README.md @@ -37,4 +37,25 @@ To avoid the most dangerous areas, you need to determine the number of points wh Consider only horizontal and vertical lines. At how many points do at least two lines overlap? +--- Part Two --- +Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines. + +Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words: + +An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3. +An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9. +Considering all lines from the above example would now produce the following diagram: + +1.1....11. +.111...2.. +..2.1.111. +...1.2.2.. +.112313211 +...1.2.... +..1...1... +.1.....1.. +1.......1. +222111.... +You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points. +Consider all of the lines. At how many points do at least two lines overlap? diff --git a/src/2021/day5/aoc.cpp b/src/2021/day5/aoc.cpp index 7a3c895..47cc423 100644 --- a/src/2021/day5/aoc.cpp +++ b/src/2021/day5/aoc.cpp @@ -1,5 +1,98 @@ #include "aoc.h" +#include <algorithm> +#include <vector> namespace aoc2021 { +static void get_number(const char** pp, int* d) { + *d = 0; + const char* p = *pp; + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *pp = p; } + +void fill(std::vector<int>& b1, std::vector<int>& b2, const line& l, int width) { + int maxx = std::max(l.p1.x, l.p2.x); + int minx = std::min(l.p1.x, l.p2.x); + int maxy = std::max(l.p1.y, l.p2.y); + int miny = std::min(l.p1.y, l.p2.y); + + if (l.p1.x == l.p2.x || l.p1.y == l.p2.y) { + for (int i = minx; i <= maxx; i++) { + for (int j = miny; j <= maxy; j++) { + // printf("[%d, %d]\n", i, j); + b1[j * width + i] += 1; + b2[j * width + i] += 1; + if (b2[j * width + i] > 1) { + printf("[%d, %d]\n", i, j); + } + } + } + } + + if (l.p1.x == l.p1.y && l.p2.x == l.p2.y) { + int j = miny; + for (int i = minx; i <= maxx; i++) { + b2[j * width + i] += 1; + if (b2[j * width + i] > 1) { + printf("[%d, %d]\n", i, j); + } + j++; + } + } + + if (l.p1.x == l.p2.y && l.p2.x == l.p1.y) { + int j = maxy; + for (int i = minx; i <= maxx; i++) { + b2[j * width + i] += 1; + if (b2[j * width + i] > 1) { + printf("[%d, %d]\n", i, j); + } + j--; + } + } +} + +std::pair<int, int> day5(line_view file) { + std::vector<line> vs; + int maxx{INT32_MIN}; + int maxy{INT32_MIN}; + + per_line(file, [&vs, &maxx, &maxy](line_view lv) { + line l; + int* is[] = {&l.p1.x, &l.p1.y, &l.p2.x, &l.p2.y}; + const char* p = lv.line; + int i{0}; + while (p < lv.line + lv.length) { + if (*p >= '0' && *p <= '9') { + get_number(&p, is[i++]); + } + p++; + } + maxx = std::max(maxx, std::max(l.p1.x, l.p2.x)); + maxy = std::max(maxy, std::max(l.p1.y, l.p2.y)); + vs.push_back(l); + return true; + }); + + maxx += 1; + maxy += 1; + + printf("%d, %d\n", maxx, maxy); + std::vector<int> board1(maxx * maxy, 0); + std::vector<int> board2(maxx * maxy, 0); + for (auto& l : vs) { + fill(board1, board2, l, maxx); + } + + int t1{0}; + int t2{0}; + std::for_each(board1.begin(), board1.end(), [&t1](int x) { t1 += int(x >= 2); }); + std::for_each(board2.begin(), board2.end(), [&t2](int x) { t2 += int(x >= 2); }); + return {t1, t2}; +} + +} // namespace aoc2021 diff --git a/src/2021/day5/aoc.h b/src/2021/day5/aoc.h index 7733f90..c57f439 100644 --- a/src/2021/day5/aoc.h +++ b/src/2021/day5/aoc.h @@ -4,4 +4,14 @@ namespace aoc2021 { -} +struct line { + struct point { + int x; + int y; + }; + point p1; + point p2; +}; + +std::pair<int, int> day5(line_view); +} // namespace aoc2021 diff --git a/src/2021/day5/input0 b/src/2021/day5/input0 new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/src/2021/day5/input0 @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 |