Skip to main content
1 of 5
BrainSteel
  • 5.5k
  • 3
  • 19
  • 39

#C, 315 bytes

t,i;double z,o,w,h,x,y,k,a,b,c;double g(N,S)double N,S[][2];{for(t=0;t<N;t++)k+=S[t][1];k/=N;for(i=0;i<9;i++){z=o=w=h=0;for(t=0;t<N;t++)x=S[t][0],y=S[t][1],a=y-k,b=sqrt(x*x+a*a),c=k*k-2*k*y+x*x+y*y,z+=b,o+=-a/b,w+=x*x/pow(c,1.5),h+=3*x*x*a/pow(c,2.5);a=h/2;b=w-h*k;c=o-w*k+a*k*k;k=(-b+sqrt(b*b-4*a*c))/h;}return k;}

This is far from pretty, and it's not short either (though, looking at that Python solution... I'm not done golfing). I figured since I'm not going to win the length contest, I can try to win the accuracy contest! The code is probably an order of magnitude or two faster than the bruteforce solution, and relies on a bit of mathematical tomfoolery.

We define a function g(N,S) which takes as input the number of houses, N, and an array of houses S[][2].

Here it is unraveled, with a test case:

t,i;
double z,o,w,h,x,y,k,a,b,c;
double g(N,S)double N,S[][2];{
    for(t=0;t<N;t++)
        k+=S[t][1];
    k/=N;
    for(i=0;i<9;i++){
        z=o=w=h=0;
        for(t=0;t<N;t++)
            x=S[t][0],
            y=S[t][1],
            a=y-k,
            b=sqrt(x*x+a*a),
            c=k*k-2*k*y+x*x+y*y,
            z+=b,
            o+=-a/b,
            w+=x*x/pow(c,1.5),
            h+=3*x*x*a/pow(c,2.5);
        a=h/2;
        b=w-h*k;
        c=o-w*k+a*k*k;
        k=(-b+sqrt(b*b-4*a*c))/h;
    }
    return k;
}


int main(int argc, char** argv) {
    /* Our test case */
    double test[2][2] = {
        {5.7, 3.2},
        {8.9, 8.1}
    };
    

    printf("%.20lf\n", g(2, test));
    return 0;
}

Which outputs:

5.11301369863013732697

If anyone asks, I'll be happy to explain the method once I'm satisfied with the golfing.

BrainSteel
  • 5.5k
  • 3
  • 19
  • 39