This commit is contained in:
Alfred Melch 2018-12-06 12:43:48 +01:00
parent 82f106b186
commit 7e39e55e98
5 changed files with 260 additions and 0 deletions

88
day-06/01.py Normal file
View File

@ -0,0 +1,88 @@
#!/usr/bin/env python3
from pprint import pprint
with open('input.txt', 'r') as f:
coords = [x.strip().split(', ') for x in f.readlines()]
coords = [tuple(map(lambda y: int(y), x)) for x in coords]
# print(coords)
def distance(p1, p2):
"""Manhattan distance"""
return abs(p2[0] - p1[0]) + abs(p2[1] - p1[1])
def get_closest_idx(coords, point):
"""Returns the index of the point that is closest to the point or None"""
distances = [distance(x, point) for x in coords]
min_dist = min(distances)
return distances.index(min_dist) if distances.count(min_dist) == 1 else None
def is_infinite_area(coords, idx, delta=10000):
"""Determines if point coords[idx] has infinite area
A point has infinite area if it is closest to any of the four points:
* (x, y + delta)
* (x, y - delta)
* (x + delta, y)
* (x - delte, y)
"""
point = coords[idx]
far_points = [
(point[0], point[1] + delta),
(point[0], point[1] - delta),
(point[0] + delta, point[1]),
(point[0] - delta, point[1])
]
for far in far_points:
if get_closest_idx(coords, far) == idx:
return True
return False
def get_circle(point, radius):
"""Returns an array of all points with distance radius to point"""
if radius == 0:
return [point]
x = point[0]
y = point[1]
d_x = radius
d_y = 0
circle = list()
while d_y < radius:
circle.append((x + d_x, y + d_y))
circle.append((x - d_x, y - d_y))
circle.append((x + d_y, y - d_x))
circle.append((x - d_y, y + d_x))
d_x -= 1
d_y += 1
return circle
def main():
areas = list()
area_map = dict()
for idx, point in enumerate(coords):
if is_infinite_area(coords, idx):
continue
is_relevant = True
radius = 0
area = 0
while is_relevant:
# print(f'Radius {radius}')
is_relevant = False
for p2 in get_circle(point, radius):
if idx == get_closest_idx(coords, p2):
is_relevant = True
area += 1
radius += 1
areas.append(area)
print(f'Point {point}: Area: {area}')
print('Largest Area', max(areas))
main()

28
day-06/02.py Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python3
from pprint import pprint
with open('input.txt', 'r') as f:
coords = [x.strip().split(', ') for x in f.readlines()]
coords = [tuple(map(lambda y: int(y), x)) for x in coords]
# print(coords)
def distance(p1, p2):
"""Manhattan distance"""
return abs(p2[0] - p1[0]) + abs(p2[1] - p1[1])
min_x = min([x[0] for x in coords])
max_x = max([x[0] for x in coords])
min_y = min([x[1] for x in coords])
max_y = max([x[1] for x in coords])
res = 0
for x in range(min_x, max_x + 1):
for y in range(min_y, max_y + 1):
point = (x, y)
dist_sum = sum([distance(x, point) for x in coords])
if dist_sum < 10000:
res += 1
print(res)

92
day-06/README.md Normal file
View File

@ -0,0 +1,92 @@
--- 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?
Your puzzle answer was 3989.
--- 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?
Your puzzle answer was 49715.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

50
day-06/input.txt Normal file
View File

@ -0,0 +1,50 @@
224, 153
176, 350
353, 241
207, 59
145, 203
123, 210
113, 203
191, 241
172, 196
209, 249
260, 229
98, 231
305, 215
258, 141
337, 282
156, 140
325, 197
179, 279
283, 233
317, 150
305, 245
67, 109
251, 140
245, 59
173, 105
59, 173
257, 70
269, 110
102, 162
179, 180
324, 112
357, 311
317, 245
239, 112
321, 220
133, 97
334, 99
117, 102
133, 112
222, 316
68, 296
150, 287
263, 263
66, 347
128, 118
63, 202
68, 236
264, 122
77, 243
92, 110

2
day-06/solutions.txt Normal file
View File

@ -0,0 +1,2 @@
01: 3989
02: 49715