day 9! killme
This commit is contained in:
88
9/ex2.py
Normal file
88
9/ex2.py
Normal file
@@ -0,0 +1,88 @@
|
||||
def on_boundary(point, loop): # dk if this even needed but fucked if im removing it
|
||||
x, y = point
|
||||
for i in range(len(loop)):
|
||||
x1, y1 = loop[i]
|
||||
x2, y2 = loop[(i + 1) % len(loop)]
|
||||
|
||||
if (x1 == x2 == x) and (min(y1, y2) <= y <= max(y1, y2)):
|
||||
return True
|
||||
if (y1 == y2 == y) and (min(x1, x2) <= x <= max(x1, x2)):
|
||||
return True
|
||||
return False
|
||||
|
||||
def point_in_loop(point, loop): # ray-casting algorithm - deternines if point is in loop
|
||||
x, y = point
|
||||
inside = False
|
||||
n = len(loop)
|
||||
for i in range(n):
|
||||
xi, yi = loop[i]
|
||||
xj, yj = loop[(i + 1) % n]
|
||||
if (yi > y) != (yj > y):
|
||||
xints = xi + (y - yi) * (xj - xi) / (yj - yi)
|
||||
if x <= xints:
|
||||
inside = not inside
|
||||
return inside
|
||||
|
||||
def rect_in_loop(v1, v2, loop_vertices):
|
||||
rx_min, rx_max = min(v1[0], v2[0]), max(v1[0], v2[0])
|
||||
ry_min, ry_max = min(v1[1], v2[1]), max(v1[1], v2[1])
|
||||
|
||||
if rx_min == rx_max or ry_min == ry_max: # single line cases!
|
||||
mid_x = (rx_min + rx_max) / 2
|
||||
mid_y = (ry_min + ry_max) / 2
|
||||
return point_in_loop((mid_x, mid_y), loop_vertices)
|
||||
|
||||
# rule 1: is center in loop?
|
||||
center_point = ((rx_min + rx_max) / 2, (ry_min + ry_max) / 2)
|
||||
|
||||
if not point_in_loop(center_point, loop_vertices):
|
||||
return False
|
||||
|
||||
# rule 2: are any vertices of the loop inside the rectangle?, if so you done fucked up a-aron
|
||||
for (vx, vy) in loop_vertices:
|
||||
if rx_min < vx < rx_max and ry_min < vy < ry_max:
|
||||
return False
|
||||
|
||||
# rule 3: any edge of the loop cross the rectangle fully?
|
||||
# ACCORDING TO MY CALCULATIONS THIS SHOULD NOT BE NEEDED YET IT DOES NOT WORK WITHOUT IT SO HERE WE ARE
|
||||
n = len(loop_vertices)
|
||||
for i in range(n):
|
||||
p1 = loop_vertices[i]
|
||||
p2 = loop_vertices[(i + 1) % n]
|
||||
|
||||
# verical edges
|
||||
if p1[0] == p2[0]:
|
||||
edge_x = p1[0]
|
||||
y_start, y_end = min(p1[1], p2[1]), max(p1[1], p2[1])
|
||||
|
||||
if rx_min < edge_x < rx_max:
|
||||
if y_start <= ry_min and y_end >= ry_max:
|
||||
return False
|
||||
|
||||
# horizontal edges
|
||||
elif p1[1] == p2[1]:
|
||||
edge_y = p1[1]
|
||||
x_start, x_end = min(p1[0], p2[0]), max(p1[0], p2[0])
|
||||
|
||||
if ry_min < edge_y < ry_max:
|
||||
if x_start <= rx_min and x_end >= rx_max:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
valid_rects = []
|
||||
|
||||
with open('input.txt', 'r') as file:
|
||||
lines = file.readlines()
|
||||
coords = [tuple(map(int, line.strip().split(','))) for line in lines]
|
||||
|
||||
for i in range(len(coords)):
|
||||
for j in range(i + 1, len(coords)):
|
||||
v1 = coords[i]
|
||||
v2 = coords[j]
|
||||
|
||||
if v1[0] != v2[0] and v1[1] != v2[1]:
|
||||
if rect_in_loop(v1, v2, coords):
|
||||
valid_rects.append((v1, v2, (abs(v2[0] - v1[0]) + 1) * (abs(v2[1] - v1[1]) + 1)))
|
||||
|
||||
print("Largest area rectangle:", max(valid_rects, key=lambda x: x[2]) if valid_rects else None) # pls santa pls pls pls i swear i will never say anything bad abt your elves again
|
||||
Reference in New Issue
Block a user