This commit is contained in:
Ben
2018-12-07 13:08:52 +00:00
parent 60bcb4ffd5
commit 6e77b7a8a5
4 changed files with 262 additions and 0 deletions

BIN
AdventOfCode2018/6thDay/a.out Executable file

Binary file not shown.

View File

@@ -0,0 +1,81 @@
--- Day 6: Chronal Coordinates ---
The device on your wrist beeps several times, and once again you feel like you're falling.
"Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."
The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.
If they're dangerous, maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.
Using only the Manhattan distance, determine the area around each coordinate by counting the number of integer X,Y locations that are closest to that coordinate (and aren't tied in distance to any other coordinate).
Your goal is to find the size of the largest area that isn't infinite. For example, consider the following list of coordinates:
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9
If we name these coordinates A through F, we can draw them on a grid, putting 0,0 at the top left:
..........
.A........
..........
........C.
...D......
.....E....
.B........
..........
..........
........F.
This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:
aaaaa.cccc
aAaaa.cccc
aaaddecccc
aadddeccCc
..dDdeeccc
bb.deEeecc
bBb.eeee..
bbb.eeefff
bbb.eeffff
bbb.ffffFf
Locations shown as . are equally far from two or more coordinates, and so they don't count as being closest to any.
In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself). Therefore, in this example, the size of the largest area is 17.
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?

View File

@@ -0,0 +1,50 @@
194, 200
299, 244
269, 329
292, 55
211, 63
123, 311
212, 90
292, 169
359, 177
354, 95
101, 47
95, 79
95, 287
294, 126
81, 267
330, 78
202, 165
225, 178
266, 272
351, 326
180, 62
102, 178
151, 101
343, 145
205, 312
74, 193
221, 56
89, 89
242, 172
59, 138
83, 179
223, 88
297, 234
147, 351
226, 320
358, 338
321, 172
54, 122
263, 165
126, 341
64, 132
264, 306
72, 202
98, 49
238, 67
310, 303
277, 281
222, 318
357, 169
123, 225

View File

@@ -0,0 +1,131 @@
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
struct Point
{
int64_t x, y;
Point(const int64_t &X, const int64_t &Y) : x(X), y(Y) {}
Point() = default;
};
int64_t distance(const Point &a, const Point &b)
{
return std::abs(a.x - b.x) + std::abs(a.y - b.y);
}
std::istream &operator>>(std::istream &is, Point &p)
{
char c;
is >> p.x >> c >> p.y;
return is;
}
std::ostream &operator<<(std::ostream &os, Point &p)
{
char c;
os << p.x << ", " << p.y;
return os;
}
size_t min_index(const size_t &invalid, const Point &point,
const std::vector<Point> &points)
{
size_t result;
int64_t min_dist(std::numeric_limits<int64_t>::max());
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(point, points[p]));
if(min_dist > d)
{
min_dist = d;
result = p;
}
else if(min_dist == d)
{
result = invalid;
}
}
return result;
}
int main(int argc, char *argv[])
{
std::ifstream infile(argv[1]);
std::vector<Point> points(std::istream_iterator<Point>(infile), {});
int64_t min_x(std::numeric_limits<int64_t>::max()), min_y(min_x),
max_x(std::numeric_limits<int64_t>::min()), max_y(max_x);
for(auto &p : points)
{
min_x = std::min(min_x, p.x);
min_y = std::min(min_y, p.y);
max_x = std::max(max_x, p.x);
max_y = std::max(max_y, p.y);
}
int64_t width(max_x - min_x + 1), height(max_y - min_y + 1);
const size_t invalid(points.size());
std::vector<int64_t> num_claimed(points.size() + 1, 0);
std::set<size_t> invalid_points;
for(int64_t x = min_x; x <= max_x; ++x)
{
invalid_points.insert(min_index(invalid, Point(x, min_y), points));
invalid_points.insert(min_index(invalid, Point(x, max_y), points));
}
for(int64_t y = min_y; y <= max_y; ++y)
{
invalid_points.insert(min_index(invalid, Point(min_x, y), points));
invalid_points.insert(min_index(invalid, Point(max_x, y), points));
}
for(int64_t x = 0; x < width; ++x)
for(int64_t y = 0; y < height; ++y)
{
int64_t min_dist(std::numeric_limits<int64_t>::max());
size_t min_index;
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(Point(x + min_x, y + min_y), points[p]));
if(min_dist > d)
{
min_dist = d;
min_index = p;
}
else if(min_dist == d)
{
min_index = invalid;
}
}
if(invalid_points.find(min_index) == invalid_points.end())
++num_claimed[min_index];
}
std::cout << "Part 1: "
<< *std::max_element(num_claimed.begin(), num_claimed.end())
<< "\n";
int64_t area(0);
constexpr int64_t cutoff(10000);
const int64_t padding(cutoff / points.size() + 1);
const int64_t x_lower(min_x - padding), x_upper(max_x + 1 + padding),
y_lower(min_y - padding), y_upper(max_y + 1 + padding);
for(int64_t x = x_lower; x < x_upper; ++x)
for(int64_t y = y_lower; y < y_upper; ++y)
{
int64_t total_dist(0);
for(auto &point : points)
{
total_dist += distance(Point(x, y), point);
if(total_dist > cutoff)
break;
}
if(total_dist < cutoff)
++area;
}
std::cout << "Part 2: " << area << "\n";
}