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])) def surface(a,b): return (abs(a[0]-b[0])+1)*(abs(a[1]-b[1])+1) 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): 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): border.add((x,lines[i][1])) print(hotspots) 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 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 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)