Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Escape by spark856
from sympy import pi
from sympy.geometry import *
def escape(jar, fly):
w, h, d = jar
x, y, vx, vy = fly
if vy == 0: return False
now = Ray((x,y),(x+vx,y+vy))
corner = [Point(0,0),Point(0,h),Point(w,0),Point(w,h)]
wall = [Segment((0,0),(0,h)),Segment((0,0),(w,0)),Segment((w,h),(0,h)),Segment((w,h),(w,0))]
gate = Segment(((w-d)/2+1,h),(d+(w-d)/2-1,h))
for _ in range(20):
pre = now
if now.intersection(gate): return True
flag = False
for i in corner:
inter = i.intersection(now)
if inter:
if now.source==inter[0]:continue
px, py = inter[0]
vx, vy = -vx, -vy
now = Ray((px,py),(px+vx,py+vy))
flag = True
break
if flag: continue
for i in wall:
inter = i.intersection(now)
if inter:
if now.source==inter[0]:continue
px,py = inter[0]
if px in [0,w]: vx *= -1
else: vy *= -1
now = Ray((px,py),(px+vx,py+vy))
break
if now==pre:
now = now.rotate(pi)
vx, vy = -vx, -vy
return False
if __name__ == '__main__':
print("Example:")
print(escape([1000, 1000, 200], [0, 0, 100, 0]))
# These "asserts" are used for self-checking and not for an auto-testing
assert escape([1000, 1000, 200], [0, 0, 100, 0]) == False, "First"
assert escape([1000, 1000, 200], [450, 50, 0, -100]) == True, "Second"
assert escape([1000, 1000, 200], [450, 1000, 100, 0]) == False, "Third"
assert escape([1000, 1000, 200], [250, 250, -10, -50]) == False, "Fourth"
assert escape([1000, 2000, 200], [20, 35, 100, 175]) == True, "Fifth"
print("Coding complete? Click 'Check' to earn cool rewards!")
May 8, 2020