day 9 cheated kinda :c
This commit is contained in:
parent
8d7b9d1971
commit
1931c20b69
2 changed files with 83 additions and 27 deletions
72
day9/2.py
72
day9/2.py
|
|
@ -4,52 +4,74 @@ with open(r'day9/input.txt', 'r') as input:
|
||||||
def surface(a,b):
|
def surface(a,b):
|
||||||
return (abs(a[0]-b[0])+1)*(abs(a[1]-b[1])+1)
|
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)):
|
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)
|
j = (i+1)%len(lines)
|
||||||
if lines[i][0] == lines[j][0]:
|
if lines[i][0] == lines[j][0]:
|
||||||
sign = 1 if lines[i][1] <= lines[j][1] else -1
|
sign = 1 if lines[i][1] <= lines[j][1] else -1
|
||||||
for x in range(lines[i][1],lines[j][1],sign):
|
for x in range(lines[i][1],lines[j][1],sign):
|
||||||
inner.add((lines[i][0],x))
|
border.add((lines[i][0],x))
|
||||||
else:
|
else:
|
||||||
sign = 1 if lines[i][0] <= lines[j][0] else -1
|
sign = 1 if lines[i][0] <= lines[j][0] else -1
|
||||||
for x in range(lines[i][0],lines[j][0],sign):
|
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]:
|
print(hotspots)
|
||||||
init = (lines[0][0]-1,lines[0][1]+1)
|
|
||||||
else:
|
|
||||||
init = (lines[0][0]+1,lines[0][1]+1)
|
|
||||||
|
|
||||||
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]
|
def is_rect_inside(a,b):
|
||||||
neigh = [[-1,0],[-1,-1],[0,-1],[1,0],[0,1],[1,1],[-1,1],[1,-1]]
|
precision = 1000
|
||||||
while len(L):
|
upper_left = (min(a[0],b[0]),min(a[1],b[1]))
|
||||||
sommet = L.pop(0)
|
lower_right = (max(a[0],b[0]),max(a[1],b[1]))
|
||||||
if sommet not in inner:
|
upper_right = (max(a[0],b[0]),min(a[1],b[1]))
|
||||||
inner.add(sommet)
|
lower_left = (min(a[0],b[0]),max(a[1],b[1]))
|
||||||
for nei in neigh:
|
upper_border = (range(upper_left[0],upper_right[1],precision),[upper_left[1]])
|
||||||
new = (sommet[0]+nei[0],sommet[1]+nei[1])
|
lower_border = (range(lower_left[0],lower_right[0],precision),[lower_left[1]])
|
||||||
if new not in inner and new not in L:
|
right_border = ([lower_right[0]], range(upper_right[1],lower_right[1],precision))
|
||||||
L.append(new)
|
left_border = ([lower_left[0]],range(upper_left[1],lower_left[0],precision))
|
||||||
|
|
||||||
print("init done")
|
for border_2 in [upper_border,lower_border,left_border,right_border]:
|
||||||
|
for i in border_2[0]:
|
||||||
def is_inside(a,b):
|
for j in border_2[1]:
|
||||||
for i in range(min(a[0],b[0]),max(a[0],b[0])+1):
|
if (i,j) not in border and not is_point_inside((i,j)):
|
||||||
for j in range(min(a[1],b[1]),max(a[1],b[1])+1):
|
|
||||||
if (i,j) not in inner:
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
best = surface(lines[0],lines[1])
|
best = surface(lines[0],lines[1])
|
||||||
|
|
||||||
for i in range(len(lines)):
|
for i in range(len(lines)):
|
||||||
|
print(i,len(lines))
|
||||||
for j in range(i):
|
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]))
|
best = max(best,surface(lines[i],lines[j]))
|
||||||
|
print("new best found")
|
||||||
|
|
||||||
print(best)
|
print(best)
|
||||||
|
|
||||||
|
|
|
||||||
34
day9/2cheat.py
Normal file
34
day9/2cheat.py
Normal file
|
|
@ -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)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue