edge based day 6 part 1 wip

This commit is contained in:
Xevion
2019-07-27 00:06:13 -06:00
parent 71cfd47b69
commit 46d3ab79ea

View File

@@ -1,14 +1,15 @@
import os, sys, collections
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
1, 6
8, 3
3, 4
5, 5
8, 9""".split('\n')
points = list(map(lambda item : [int(num.strip()) for num in item.split(', ')], data))
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])
@@ -16,23 +17,58 @@ def manhattan(pos1, pos2):
def closest(pos):
return min(points, key=lambda point : manhattan(point, pos))
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
grid = collections.defaultdict(dict)
scores = collections.defaultdict(dict)
for pos in points:
scores[pos[0]][pos[1]] = 0
ensure = 500
for x in range(-ensure, ensure):
for y in range(-ensure, ensure):
pos = closest((x, y))
# grid[x][y] = pos
scores[pos[0]][pos[1]] += 1
def get_id(pos):
return f"{pos[0]},{pos[1]}"
def get_pos(id):
id = id.split(',')
return (id[0], id[1])
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
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])
from pprint import pprint
# pprint(scores)
true_scores = []
for x, yy in scores.items():
for y, score in yy.items():
true_scores.append(("({}, {}) : {}".format(x, y, score), score))
true_scores.sort(key=lambda item : item[1])
pprint([score[0] for score in true_scores])
# 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)
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
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))