• + 0 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...

    def solve(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 = lambda v2,v1:[x2-x1 for x2,x1 in zip(v2,v1)]
        dot = lambda v1,v2:sum(x1*x2 for x1,x2 in zip(v1,v2))
        
        pos = vdiff(pos2,pos1)
        acc = vdiff(acc2,acc1)
        
        # already in contact?
        if dot(pos,pos)<=(r1+r2)**2:
            return 'YES'
        # moving away?
        if dot(pos,acc)>=0: # not in contact now and ever
            return 'NO'
                
        # dropping the perpendicular from O on line: pos + t * acc
        t0 = - dot(pos,acc)/dot(acc,acc)
        perp = [p+t0*a for p,a in zip(pos,acc)]
        if dot(perp,perp)<=(r1+r2)**2: # check the distance
            return 'YES'
        else:
            return 'NO'