We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
was a bit annoying, that the code template didn't follow the input specification (4 lines pos1/acc1/pos2/acc2),
however solution then was easy and straightforward...
defsolve(r1,r2,pos1,acc1,pos2,acc2):# both spheres move on straight lines: pos + t^2/2 * acc# ... however doing a time transformation t' = t^2/2 this becomes pos + t' * acc# now center your coordinate system on sphere1: pos' = (pos-pos1) - t*acc1# ... then sphere2 is moving along: (pos2-pos1) + t * (acc2-acc1)# either it moves away from sphere 1, or we have to check the nearest point vdiff=lambdav2,v1:[x2-x1forx2,x1inzip(v2,v1)]dot=lambdav1,v2:sum(x1*x2forx1,x2inzip(v1,v2))pos=vdiff(pos2,pos1)acc=vdiff(acc2,acc1)# already in contact?ifdot(pos,pos)<=(r1+r2)**2:return'YES'# moving away?ifdot(pos,acc)>=0:#notincontactnowandeverreturn'NO'# dropping the perpendicular from O on line: pos + t * acct0=-dot(pos,acc)/dot(acc,acc)perp=[p+t0*aforp,ainzip(pos,acc)]ifdot(perp,perp)<=(r1+r2)**2:#checkthedistancereturn'YES'else:return'NO'
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Spheres
You are viewing a single comment's thread. Return to all comments →
was a bit annoying, that the code template didn't follow the input specification (4 lines pos1/acc1/pos2/acc2), however solution then was easy and straightforward...