Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # 2-D points implemented using (x, y) tuples
- def X(point): return point[0]
- def Y(point): return point[1]
- def manhattan_distance(p, q=(0, 0)):
- "City block distance between two points."
- return abs(X(p) - X(q)) + abs(Y(p) - Y(q))
- #day6 part1
- #Given a list of coordinates, find largest "area" of coordinates that are only closest to one point.
- #If I draw a grid, the area defined by any point on the edge of the grid will be infinite, and therefore, doesn't count
- #step 1: Create a grid. Grid boundaries will be the min/max of the x and y values
- coordinates = input_lines(6)
- def nearest_point(point, coords):
- #This function will compute the nearest coord in coords to point by MD
- min_dist, min_coord = 9999999, 0
- for i,coord in enumerate(coords):
- dist = manhattan_distance(point, coord)
- if dist<min_dist:
- min_dist, min_coord = dist, i+1
- elif dist == min_dist:
- min_coord = 0
- return min_coord
- def create_matrix(input_lines):
- #Matrix will be filled with the index of the nearest point
- #fortran indexed. 0 means two or more coordinates are equidistant
- coords = [tuple(map(int, line.strip().split(','))) for line in input_lines]
- xs, ys = list(zip(*coords))
- #make the matrix smaller by subtracting off minx, miny
- minx, maxx, miny, maxy = min(xs), max(xs), min(ys), max(ys)
- newcoords = [(c[0]-minx, c[1]-miny) for c in coords]
- matrix = np.zeros((maxx-minx+1,maxy-miny+1), dtype=int)
- it = np.nditer(matrix, flags=['multi_index'], op_flags=['readwrite'])
- for x in it:
- x[...] = nearest_point(it.multi_index, newcoords)
- return matrix
- def find_largest_area(matrix):
- #Find the largest finite area containing one number that isn't zero
- #eliminate any area touching the edges...those are infinite and don't count
- infs = set()
- sh = matrix.shape
- it = np.nditer(matrix, flags=['multi_index'])
- for x in it:
- if it.multi_index[0]==0 or it.multi_index[0]==sh[0]-1 or it.multi_index[1]==0 or it.multi_index[1]==sh[1]-1:
- infs.add(int(x))
- max_area = 0
- for i in range(np.max(matrix)):
- if i+1 in infs:
- continue
- area = np.sum(matrix==i+1)
- if area >= max_area:
- max_area = area
- return max_area
- mtx = create_matrix(coordinates)
- find_largest_area(mtx) #Answer to part 1
- #-----------------------------------
- #Day6 part2
- #Find size of area with sum of all manhattan distances less than 10000
- def total_distance_lt_thresh(point, coords, thresh=10000):
- #compute total MD to point from all coords, return True if total < thresh
- total_dist = 0
- for i,coord in enumerate(coords):
- total_dist += manhattan_distance(point, coord)
- return total_dist <thresh
- def create_matrix2(input_lines):
- #Similar to above, but instead of closest point, just give result of total_distance_lt_thresh
- #as values of the matrix. I left out the bit about subtracting off the minima, because in principle
- #this area could go further than the edges of our matrix, depending on the size of thresh
- coords = [tuple(map(int, line.strip().split(','))) for line in input_lines]
- xs,ys = list(zip(*coords))
- maxx, maxy = max(xs), max(ys)
- matrix = np.zeros((maxx+1, maxy+1), dtype=bool)
- it = np.nditer(matrix, flags=['multi_index'], op_flags=['readwrite'])
- for x in it:
- x[...] = total_distance_lt_thresh(it.multi_index, coords)
- return matrix
- #To compute the answer, just sum the entire matrix
- np.sum(create_matrix2(coordinates))
Add Comment
Please, Sign In to add comment