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