day 6 part 1 and part 2 finished

This commit is contained in:
Xevion
2019-07-27 22:27:03 -06:00
parent 46d3ab79ea
commit 2cad385b9e

View File

@@ -2,73 +2,46 @@ import os, sys, collections
path = os.path.join(sys.path[0], '..', 'input') path = os.path.join(sys.path[0], '..', 'input')
data = open(path, 'r').read().split('\n') data = open(path, 'r').read().split('\n')
data = """1, 1 # data = """1, 1
1, 6 # 1, 6
8, 3 # 8, 3
3, 4 # 3, 4
5, 5 # 5, 5
8, 9 # 8, 9
18, 10""".split('\n') # 18, 10""".split('\n')
points = list(map(lambda item : tuple((int(num.strip()) for num in item.split(', '))), data)) points = list(map(lambda item : tuple((int(num.strip()) for num in item.split(', '))), data))
def manhattan(pos1, pos2): def dist(pos1, pos2):
return abs(pos2[0] - pos1[0]) + abs(pos2[1] - pos1[1]) return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])
def closest(pos): is_infinite = set()
return min(points, key=lambda point : manhattan(point, pos)) counts = collections.defaultdict(int)
def edgeCoords(x1, y1, x2, y2): minx, miny = min(x for x,y in points), min(y for x,y in points)
coords = [] maxx, maxy = max(x for x,y in points), max(y for x,y in points)
x1, x2 = (x1, x2) if x1 < x2 else (x2, x1)
y1, y2 = (y1, y2) if y1 < y2 else (y2, y1)
for x in range(x1, x2+1):
coords.append((x1 + x, y1))
coords.append((x1 + x, y2))
for y in range(y1, y2+1):
coords.append((x1, y1 + y))
coords.append((x2, y1 + y))
return coords
def get_id(pos): for y in range(miny, maxy + 1):
return f"{pos[0]},{pos[1]}" for x in range(minx, maxx + 1):
def get_pos(id):
id = id.split(',') distances = [ (dist((x, y), point), i) for i, point in enumerate(points)]
return (id[0], id[1]) distances.sort()
def getDimensions(points): if distances[0][0] != distances[1][0]:
zippoints = list(zip(*points)) counts[distances[0][1]] += 1
minx, miny = min(zippoints[0]), min(zippoints[1])
maxx, maxy = max(zippoints[0]), max(zippoints[1])
return minx, miny, maxx, maxy
def formatted(edges, points, size=None): if x == minx or x == maxx or y == miny or y == maxy:
if not size: is_infinite.add(distances[0][1])
size = max(*getDimensions(points)) + 3
grid = [[' # ' if (x, y) in edges else ' ? ' if (x, y) in points else ' . ' for y in range(size)] for x in range(size)]
return '\n'.join([''.join(row) for row in grid])
from pprint import pprint for k in is_infinite:
# marked = {get_id(pos) : False for pos in points} counts.pop(k)
marked = collections.defaultdict(int)
minx, miny, maxx, maxy = getDimensions(points)
minx, miny, maxx, maxy = minx - 1, miny - 1, maxx + 1, maxy + 1
edges = edgeCoords(minx, miny, maxx, maxy)
for edge in edges:
close = closest(edge)
marked[get_id(close)] += True
# pprint(marked)
scores = collections.defaultdict(int) a2 = 0
for x in range(minx, maxx): for x in range(minx, maxx + 1):
for y in range(miny, maxy): for y in range(miny, maxy + 1):
close = closest((x, y)) sumdist = sum(dist((x, y), point) for point in points)
scores[get_id(close)] += 1 a2 += 1 if sumdist < 10_000 else 0
print(scores) a1 = max(counts.items(), key=lambda item : item[1])
for id in scores.keys(): a1 = a1, points[a1[0]]
if id in marked: print('Point {} had the largest non-infinite area at {} spaces.'.format(a1[1], a1[0][1]))
continue print('There are {} spaces with a distance < 10,000 to all given coordinates.'.format(a2))
print(id, scores[id])
best = max(scores.items(), key=lambda item : 0 if item[0] in marked else item[1])
print(best)
# print(formatted(edges=edgeCoords(minx, miny, maxx, maxy), points=points))