Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Shooting Range by B_dur
def shot(wall1, wall2, shot_point, later_point):
#replace this for solution
if (wall2[0]-wall1[0])*(later_point[1]-shot_point[1])==(wall2[1]-wall1[1])*(later_point[0]-shot_point[0]):
return -1
wall_center=((wall1[0]+wall2[0])/2,(wall1[1]+wall2[1])/2)
if ((wall_center[0]-shot_point[0])**2+(wall_center[1]-shot_point[1])**2)**0.5<=((wall_center[0]-later_point[0])**2+(wall_center[1]-later_point[1])**2)**0.5:
return -1
dx0,dx1=wall2[0]-wall1[0],later_point[0]-shot_point[0]
dy0,dy1=wall2[1]-wall1[1],later_point[1]-shot_point[1]
s=(wall1[1]-shot_point[1])*dx1-(wall1[0]-shot_point[0])*dy1
sm=dx0*dy1-dy0*dx1
if s<0:
s,sm=-s,-sm
cross=(wall1[0]+s*dx0/sm,wall1[1]+s*dy0/sm)
cross_to_center_norm=((wall_center[0]-cross[0])**2+(wall_center[1]-cross[1])**2)**0.5
wall_norm=((wall1[0]-wall2[0])**2+(wall1[1]-wall2[1])**2)**0.5
return 100 if cross==wall_center else 0 if cross==wall1 or cross==wall2 else -1 if cross_to_center_norm>wall_norm/2 else int(round((1-((2*cross_to_center_norm)/wall_norm))*100,0))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert shot((2, 2), (5, 7), (11, 2), (8, 3)) == 100, "1st case"
assert shot((2, 2), (5, 7), (11, 2), (7, 2)) == 0, "2nd case"
assert shot((2, 2), (5, 7), (11, 2), (8, 4)) == 29, "3th case"
assert shot((2, 2), (5, 7), (11, 2), (9, 5)) == -1, "4th case"
assert shot((2, 2), (5, 7), (11, 2), (10.5, 3)) == -1, "4th case again"
Sept. 8, 2020
Comments: