Code for Day06 part 2
This commit is contained in:
@@ -58,7 +58,43 @@ namespace AdventOfCode2018
|
||||
|
||||
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?
|
||||
|
||||
*/
|
||||
|
||||
public class Day06
|
||||
{
|
||||
public string ResolvePart1(string[] inputs)
|
||||
@@ -123,9 +159,33 @@ namespace AdventOfCode2018
|
||||
return maxArea.ToString();
|
||||
}
|
||||
|
||||
public string ResolvePart2(string[] inputs)
|
||||
public string ResolvePart2(string[] inputs, int distanceThresold)
|
||||
{
|
||||
return null;
|
||||
List<ChronoPoint> points = inputs
|
||||
.Where(input => string.IsNullOrEmpty(input) == false)
|
||||
.Select(input => ChronoPoint.FromString(input))
|
||||
.ToList();
|
||||
|
||||
int minX = points.Min(p => p.X) - 1;
|
||||
int maxX = points.Max(p => p.X) + 1;
|
||||
int minY = points.Min(p => p.Y) - 1;
|
||||
int maxY = points.Max(p => p.Y) + 1;
|
||||
|
||||
int areaInRange = 0;
|
||||
ChronoPoint samplingPoint = new ChronoPoint();
|
||||
for (int i = minX; i <= maxX; i++)
|
||||
{
|
||||
for (int j = minY; j <= maxY; j++)
|
||||
{
|
||||
samplingPoint.X = i;
|
||||
samplingPoint.Y = j;
|
||||
|
||||
int distanceSum = points.Sum(p => ChronoPoint.ManhattanDistance(samplingPoint, p));
|
||||
if (distanceSum < distanceThresold) { areaInRange++; }
|
||||
}
|
||||
}
|
||||
|
||||
return areaInRange.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace AdventOfCode2018
|
||||
string[] linesDay06 = File.ReadAllLines("inputs/Day06.txt");
|
||||
string resultPart1 = day06.ResolvePart1(linesDay06);
|
||||
Console.WriteLine("Day06 Result Part1: {0}", resultPart1);
|
||||
string resultPart2 = day06.ResolvePart2(linesDay06);
|
||||
string resultPart2 = day06.ResolvePart2(linesDay06, 10000);
|
||||
Console.WriteLine("Day06 Result Part2: {0}", resultPart2);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user