aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-16 14:16:51 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-16 14:16:51 +0800
commit51f1930d6ba21e8760d65bc51881fed7c943c983 (patch)
treeee5db06751d42c7bffa393775ca67575234b266f /src
parent04e50217ffb0288e501c1f150a13b6ca70f1367a (diff)
downloadadvent-of-code-51f1930d6ba21e8760d65bc51881fed7c943c983.tar.gz
advent-of-code-51f1930d6ba21e8760d65bc51881fed7c943c983.zip
rows cols are confusing
Diffstat (limited to 'src')
-rw-r--r--src/2018/day6/README.md30
-rw-r--r--src/2018/day6/aoc.cpp15
-rw-r--r--src/2018/day6/aoc.h36
3 files changed, 62 insertions, 19 deletions
diff --git a/src/2018/day6/README.md b/src/2018/day6/README.md
index 78571dc..b9375c6 100644
--- a/src/2018/day6/README.md
+++ b/src/2018/day6/README.md
@@ -47,4 +47,34 @@ In this example, the areas of coordinates A, B, C, and F are infinite - while no
What is the size of the largest area that isn't infinite?
+--- Part Two ---
+On the other hand, if the coordinates are safe, maybe the best you can do is try to find a region near as many coordinates as possible.
+For example, suppose you want the sum of the Manhattan distance to all of the coordinates to be less than 32. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above, the resulting region looks like this:
+
+..........
+.A........
+..........
+...###..C.
+..#D###...
+..###E#...
+.B.###....
+..........
+..........
+........F.
+In particular, consider the highlighted location 4,3 located at the top middle of the region. Its calculation is as follows, where abs() is the absolute value function:
+
+Distance to coordinate A: abs(4-1) + abs(3-1) = 5
+Distance to coordinate B: abs(4-1) + abs(3-6) = 6
+Distance to coordinate C: abs(4-8) + abs(3-3) = 4
+Distance to coordinate D: abs(4-3) + abs(3-4) = 2
+Distance to coordinate E: abs(4-5) + abs(3-5) = 3
+Distance to coordinate F: abs(4-8) + abs(3-9) = 10
+Total distance: 5 + 6 + 4 + 2 + 3 + 10 = 30
+Because the total distance to all coordinates (30) is less than 32, the location is within the region.
+
+This region, which also includes coordinates D and E, has a total size of 16.
+
+Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than 10000.
+
+What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?
diff --git a/src/2018/day6/aoc.cpp b/src/2018/day6/aoc.cpp
index 745a185..df42be0 100644
--- a/src/2018/day6/aoc.cpp
+++ b/src/2018/day6/aoc.cpp
@@ -18,12 +18,12 @@ int day6(line_view file) {
std::sort(cs.begin(), cs.end());
space_board b{maxx + 1, maxy + 1};
- printf("%d\n", b.size());
+ //printf("%d %d\n", b.width, b.height);
for (size_t i = 0; i < cs.size(); i++) {
coordinate& c = cs[i];
- auto& p = b.ps[c.y * b.rows + c.x];
+ auto& p = b.ps[c.y * b.width + c.x];
p.id = i;
p.distance = 0;
@@ -32,11 +32,10 @@ int day6(line_view file) {
auto f = [&win, &total, &b, &c, &i](size_t lap, coordinate x) {
if (lap == total.size()) {
- // printf("%zu, %d %d\n", lap - 1, total[lap - 1], win);
total.push_back(0);
}
- if (x.x >= 0 && x.x < b.cols && x.y >= 0 && x.y < b.rows) {
- auto& p = b.ps[x.y * b.rows + x.x];
+ if (x.x >= 0 && x.x < b.width && x.y >= 0 && x.y < b.height) {
+ auto& p = b.ps[x.y * b.width + x.x];
int d = x.distance(c);
if (d == p.distance) {
p.id = -2; // same distance
@@ -56,15 +55,13 @@ int day6(line_view file) {
return total[last] == 0;
};
c.traverse(f, g);
- printf("%zu %d\n", i, win);
+ // b.print();
+ // printf("%zu %d\n", i, win);
}
std::vector<int> xs(cs.size(), 0);
b.count(xs);
- std::for_each(xs.begin(), xs.end(), [](int d) { printf("%d ", d); });
- printf("\n");
-
int max{INT32_MIN};
for (auto& x : xs) {
if (x != INT32_MAX && x > max) {
diff --git a/src/2018/day6/aoc.h b/src/2018/day6/aoc.h
index 6e5b96f..2f83842 100644
--- a/src/2018/day6/aoc.h
+++ b/src/2018/day6/aoc.h
@@ -74,22 +74,38 @@ struct space_board {
// -1 no one
// -2 same distance
struct point {
- int id;
- int distance;
+ int id = -1;
+ int distance = INT32_MAX;
};
- int rows;
- int cols;
+ int width;
+ int height;
std::vector<point> ps;
- space_board(int x, int y) : rows(y), cols(x), ps(x * y, {-1, INT32_MAX}){};
+ space_board(int x, int y) : width(x), height(y), ps(x * y){};
- int size() const noexcept { return rows * cols; }
+ int size() const noexcept { return width * height; }
+
+ void print() const noexcept {
+ for (size_t i = 0; i < ps.size(); i++) {
+ if (i % width == 0) {
+ printf("\n");
+ }
+ char c = ps[i].id >= 0 ? 'a' + ps[i].id : '.';
+ if (ps[i].distance == 0) {
+ c = c - 32;
+ }
+ std::cout << c;
+ }
+ }
void count(std::vector<int>& area) const noexcept {
- for (int y = 0; y < rows; y++) {
- for (int x = 0; x < cols; x++) {
- auto& p = ps[y * rows + x];
- if (x == 0 || y == 0 || x == cols - 1 || y == rows - 1) {
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ auto& p = ps[y * width + x];
+ if (p.id == -2) {
+ continue;
+ }
+ if (x == 0 || y == 0 || x == width - 1 || y == height - 1) {
area[p.id] = INT32_MAX;
}
if (area[p.id] < INT32_MAX) {