Just compute distances from 144,84. You can avoid roundoff error by using distance squared and keeping things in integers. Here is some python code: # Function to get the square of the distances for the stones def getDists(): nums = list(map(int, input().split())) res = [1000000000] for i in range(0,nums[0]): x = nums[1+2*i] y = nums[2+2*i] dx = x-144 dy = y-84 res.append(dx*dx+dy*dy) return res def main(): red, yellow = 0, 0 for _ in range(10): red_dists = getDists() yellow_dists = getDists() clr = min(x for x in red_dists) cly = min(x for x in yellow_dists) if clr < cly: for x in red_dists: red += x < cly else: for x in yellow_dists: yellow += x < clr print(red, yellow) if __name__ == "__main__": main()