diff --git a/day9/2.py b/day9/2.py index 853a8eb..42914cb 100644 --- a/day9/2.py +++ b/day9/2.py @@ -4,52 +4,74 @@ with open(r'day9/input.txt', 'r') as input: def surface(a,b): return (abs(a[0]-b[0])+1)*(abs(a[1]-b[1])+1) -inner = set() +dim = [100000,100000] + +hotspots = set() + +border = set() for i in range(len(lines)): + hotspots.add(lines[i][0]) + hotspots.add(lines[i][0]-1) + hotspots.add(lines[i][0]+1) + hotspots.add(lines[i][1]) + hotspots.add(lines[i][1]-1) + hotspots.add(lines[i][1]+1) j = (i+1)%len(lines) if lines[i][0] == lines[j][0]: sign = 1 if lines[i][1] <= lines[j][1] else -1 for x in range(lines[i][1],lines[j][1],sign): - inner.add((lines[i][0],x)) + border.add((lines[i][0],x)) else: sign = 1 if lines[i][0] <= lines[j][0] else -1 for x in range(lines[i][0],lines[j][0],sign): - inner.add((x,lines[i][1])) + border.add((x,lines[i][1])) -if lines[0][0] == lines[1][0]: - init = (lines[0][0]-1,lines[0][1]+1) -else: - init = (lines[0][0]+1,lines[0][1]+1) +print(hotspots) -print(init) +def is_point_inside(a): + # up, down, left, right + ranges = [[0,a[1]],[a[1],dim[1]],[a[0],dim[0]],[0,a[0]]] + for j in range(4): + count = 0 + last = False + for i in hotspots: + if i < ranges[j][0] or i > ranges[j][1]: continue + coord = (a[0],i) if j < 2 else (i,a[1]) + if coord in border: last = True + elif last and coord not in border: + last = False + count +=1 + if count%2 == 0: + return False + return True -L=[init] -neigh = [[-1,0],[-1,-1],[0,-1],[1,0],[0,1],[1,1],[-1,1],[1,-1]] -while len(L): - sommet = L.pop(0) - if sommet not in inner: - inner.add(sommet) - for nei in neigh: - new = (sommet[0]+nei[0],sommet[1]+nei[1]) - if new not in inner and new not in L: - L.append(new) - -print("init done") - -def is_inside(a,b): - for i in range(min(a[0],b[0]),max(a[0],b[0])+1): - for j in range(min(a[1],b[1]),max(a[1],b[1])+1): - if (i,j) not in inner: - return False +def is_rect_inside(a,b): + precision = 1000 + upper_left = (min(a[0],b[0]),min(a[1],b[1])) + lower_right = (max(a[0],b[0]),max(a[1],b[1])) + upper_right = (max(a[0],b[0]),min(a[1],b[1])) + lower_left = (min(a[0],b[0]),max(a[1],b[1])) + upper_border = (range(upper_left[0],upper_right[1],precision),[upper_left[1]]) + lower_border = (range(lower_left[0],lower_right[0],precision),[lower_left[1]]) + right_border = ([lower_right[0]], range(upper_right[1],lower_right[1],precision)) + left_border = ([lower_left[0]],range(upper_left[1],lower_left[0],precision)) + + for border_2 in [upper_border,lower_border,left_border,right_border]: + for i in border_2[0]: + for j in border_2[1]: + if (i,j) not in border and not is_point_inside((i,j)): + return False return True best = surface(lines[0],lines[1]) for i in range(len(lines)): + print(i,len(lines)) for j in range(i): - if is_inside(lines[i],lines[j]): + if surface(lines[i],lines[j]) > best and is_rect_inside(lines[i],lines[j]): best = max(best,surface(lines[i],lines[j])) + print("new best found") print(best) diff --git a/day9/2cheat.py b/day9/2cheat.py new file mode 100644 index 0000000..1af0160 --- /dev/null +++ b/day9/2cheat.py @@ -0,0 +1,34 @@ +import shapely +from shapely.geometry import Point, Polygon + +with open(r'day9/input.txt', 'r') as input: + lines = list(map(lambda x: list(map(lambda y: int(y),x.split(','))),input.read().split('\n')[:-1])) + +points = [] + +def surface(a,b): + return (abs(a.x-b.x)+1)*(abs(a.y-b.y)+1) + + +for line in lines: + points.append(Point(line[0],line[1])) + +points.append(points[0]) + +polygon = Polygon([p.x,p.y] for p in points) + +bestarea = shapely.area(shapely.box(0,0,0,0)) + +for i in range(len(points)): + print(i) + for j in range(i): + minx = min(points[i].x,points[j].x) + miny = min(points[i].y,points[j].y) + maxx = max(points[i].x,points[j].x) + maxy = max(points[i].y,points[j].y) + box = shapely.box(minx,miny,maxx,maxy) + box_surface = surface(points[i],points[j]) + if box_surface > bestarea and shapely.contains(polygon,box): + bestarea = box_surface +print(bestarea) +