Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Escape by Sim0000
def escape(jar, fly):
W, H, d = jar
x, y, vx, vy = fly
if vx == 0 and vy and abs(2 * x - W) < d: return True
if vx == 0 or vy == 0: return False
for _ in range(20):
x2 = W if vx >= 0 else 0
y2 = y + vy * (x2 - x) / vx
if not 0 <= y2 <= H:
y2 = H if vy >= 0 else 0
x2 = x + vx * (y2 - y) / vy
if vy > 0 and abs(2 * x2 - W) < d: return True
x, y = x2, y2
if x in (0, W): vx = -vx
if y in (0, H): vy = -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!")
Dec. 3, 2020
Comments: