Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Algebra+Lambdas solution in Speedy category for Shooting Range by PythonWithPI
shot=lambda wall1, wall2, shot_point, later_point,score=lambda wallRayTime,shotDirection: 100-abs(200*wallRayTime-100) if 0<=wallRayTime<=1 and shotDirection(wallRayTime) else -1\
:(lambda x3=wall1[0],x4=wall2[0],x1=shot_point[0],x2=later_point[0],y3=wall1[1],y4=wall2[1],y1=shot_point[1],y2=later_point[1]:
round(score((y2*x3-y1*x3-y2*x1+y1*x1-x2*y3+x1*y3+x2*y1-x1*y1)/(x2*y4-x2*y3-x1*y4+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3) if x2*y4-x2*y3-x1*y4+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3!=0 else -1,(lambda t2:((x3+(x4-x3)*t2-x1)/(x2-x1)>=1) if (x2!=x1) else ( (y3+(y4-y3)*t2-y1)/(y2-y1)>=1)) )))()
#Line intersection: linear algebra
#parametric equations:
#x=x1+(x2-x1)*t1
#y=y1+(y2-y1)*t1
#y3-4 and x3-4 are wall point information, y1-2 and x1-2 are shot info, 2 means later
#t1 is shot ray time
#t2 is wall ray time
#x1+(x2-x1)*t1=x3+(x4-x3)*t2 find intersection equations
#y1+(y2-y1)*t1=y3+(y4-y3)*t2
#
#Fraction(x3+(x4-x3)*t2-x1,x2-x1)=t1 solve for t1
#Fraction(y3+(y4-y3)*t2-y1,y2-y1)=t1
#
#Fraction(x3+(x4-x3)*t2-x1,x2-x1)=Fraction(y3+(y4-y3)*t2-y1,y2-y1) cross multiply
#(y2-y1)*(x3+(x4-x3)*t2-x1)=(x2-x1)*(y3+(y4-y3)*t2-y1)
#y2*x3-y1*x3+y2*x4*t2-y2*x3*t2-y1*x4*t2+y1*x3*t2-y2*x1+y1*x1=x2*y3-x1*y3+x2*y4*t2-x2*y3*t2-x1*y4*t2+x1*y3*t2-x2*y1+x1*y1
#distribute ;)
#y2*x3-y1*x3-y2*x1+y1*x1-x2*y3+x1*y3+x2*y1-x1*y1=x2*y4*t2-x2*y3*t2-x1*y4*t2+x1*y3*t2-y2*x4*t2+y2*x3*t2+y1*x4*t2-y1*x3*t2
#rearrange with t2 terms on right
#y2*x3-y1*x3-y2*x1+y1*x1-x2*y3+x1*y3+x2*y1-x1*y1=t2*(x2*y4-x2*y3-x1*y4+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3)
#Fraction(y2*x3-y1*x3-y2*x1+y1*x1-x2*y3+x1*y3+x2*y1-x1*y1,x2*y4-x2*y3-x1*y4+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3)=t2
#
#function from wall ray time to score:
#100-abs(200x-100) if 0<=x<=1 else -1
#find t1 to test shooting direction:
#t1=(x3+(x4-x3)*t2-x1)/(x2-x1)
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. 19, 2017