r/dailyprogrammer • u/jnazario 2 0 • Sep 16 '15
[2015-09-16] Challenge #232 [Intermediate] Where Should Grandma's House Go?
Description
My grandmother and I are moving to a new neighborhood. The houses haven't yet been built, but the map has been drawn. We'd like to live as close together as possible. She makes some outstanding cookies, and I love visiting her house on the weekend for delicious meals - my grandmother is probably my favorite cook!
Please help us find the two lots that are closest together so we can build our houses as soon as possible.
Example Input
You'll be given a single integer, N, on a line, then N lines of Cartesian coordinates of (x,y) pairs. Example:
16
(6.422011725438139, 5.833206713226367)
(3.154480546252892, 4.063265532639129)
(8.894562467908552, 0.3522346393034437)
(6.004788746281089, 7.071213090379764)
(8.104623252768594, 9.194871763484924)
(9.634479418727688, 4.005338324547684)
(6.743779037952768, 0.7913485528735764)
(5.560341970499806, 9.270388445393506)
(4.67281620242621, 8.459931892672067)
(0.30104230919622, 9.406899285442249)
(6.625930036636377, 6.084986606308885)
(9.03069534561186, 2.3737246966612515)
(9.3632392904531, 1.8014711293897012)
(2.6739636897837915, 1.6220708577223641)
(4.766674944433654, 1.9455404764480477)
(7.438388978141802, 6.053689746381798)Example Output
Your program should emit the two points of (x,y) pairs that are closest together. Example:
(6.625930036636377,6.084986606308885) (6.422011725438139,5.833206713226367)Challenge Input
100
(5.558305599411531, 4.8600305440370475)
(7.817278884196744, 0.8355602049697197)
(0.9124479406145247, 9.989524754727917)
(8.30121530830896, 5.0088455259181615)
(3.8676289528099304, 2.7265254619302493)
(8.312363982415834, 6.428977658434681)
(2.0716308507467573, 4.39709962385545)
(4.121324567374094, 2.7272406843892005)
(9.545656436023116, 2.874375810978397)
(2.331392166597921, 0.7611494627499826)
(4.241235371900736, 5.54066919094827)
(3.521595862125549, 6.799892867281735)
(7.496600142701988, 9.617336260521792)
(2.5292596863427796, 4.6514954819640035)
(8.9365560770944, 8.089768281770253)
(8.342815293157892, 1.3117716484643926)
(6.358587371849396, 0.7548433481891659)
(1.9085858694489566, 1.2548184477302327)
(4.104650644200331, 5.1772760616934645)
(6.532092345214275, 8.25365480511137)
(1.4484096875115393, 4.389832854018496)
(9.685268864302843, 5.7247619715577915)
(7.277982280818066, 3.268128640986726)
(2.1556558331381104, 7.440500993648994)
(5.594320635675139, 6.636750073337665)
(2.960669091428545, 5.113509430176043)
(4.568135934707252, 8.89014754737183)
(4.911111477474849, 2.1025489963335673)
(8.756483469153423, 1.8018956531996244)
(1.2275680076218365, 4.523940697190396)
(4.290558055568554, 5.400885500781402)
(8.732488819663526, 8.356454134269345)
(6.180496817849347, 6.679672206972223)
(1.0980556346150605, 9.200474664842345)
(6.98003484966205, 8.22081445865494)
(1.3008030292739836, 2.3910813486547466)
(0.8176167873315643, 3.664910265751047)
(4.707575761419376, 8.48393210654012)
(2.574624846075059, 6.638825467263861)
(0.5055608733353167, 8.040212389937379)
(3.905281319431256, 6.158362777150526)
(6.517523776426172, 6.758027776767626)
(6.946135743246488, 2.245153765579998)
(6.797442280386309, 7.70803829544593)
(0.5188505776214936, 0.1909838711203915)
(7.896980640851306, 4.366680008699691)
(1.2404651962738256, 5.963706923183244)
(7.9085889544911945, 3.501907219426883)
(4.829123686370425, 6.116328436853205)
(8.703429477346157, 2.494600359615746)
(6.9851545945688684, 9.241431992924019)
(1.8865556630758573, 0.14671871143506765)
(4.237855680926536, 1.4775578026826663)
(3.8562761635286913, 6.487067768929168)
(5.8278084663109375, 5.98913080157908)
(8.744913811001137, 8.208176389217819)
(1.1945941254992176, 5.832127086137903)
(4.311291521846311, 7.670993787538297)
(4.403231327756983, 6.027425952358197)
(8.496020365319831, 5.059922514308242)
(5.333978668303457, 5.698128530439982)
(9.098629270413424, 6.8347773139334675)
(7.031840521893548, 6.705327830885423)
(9.409904685404713, 6.884659612909266)
(4.750529413428252, 7.393395242301189)
(6.502387440286758, 7.5351527902895965)
(7.511382341946669, 6.768903823121008)
(7.508240643932754, 6.556840482703067)
(6.997352867756065, 0.9269648538573272)
(0.9422251775272161, 5.103590106844054)
(0.5527353428303805, 8.586911807313664)
(9.631339754852618, 2.6552168069445736)
(5.226984134025007, 2.8741061109013555)
(2.9325669592417802, 5.951638270812146)
(9.589378643660075, 3.2262646648108895)
(1.090723228724918, 1.3998921986217283)
(8.364721356909339, 3.2254754023019148)
(0.7334897173512944, 3.8345650175295143)
(9.715154631802577, 2.153901162825511)
(8.737338862432715, 0.9353297864316323)
(3.9069371008200218, 7.486556673108142)
(7.088972421888375, 9.338974320116852)
(0.5043493283135492, 5.676095496775785)
(8.987516578950164, 2.500145166324793)
(2.1882275188267752, 6.703167722044271)
(8.563374867122342, 0.0034374051899066504)
(7.22673935541426, 0.7821487848811326)
(5.305665745194435, 5.6162850431000875)
(3.7993107636948267, 1.3471479136817943)
(2.0126321055951077, 1.6452950898125662)
(7.370179253675236, 3.631316127256432)
(1.9031447730739726, 8.674383934440593)
(8.415067672112773, 1.6727089997072297)
(6.013170692981694, 7.931049747961199)
(0.9207317960126238, 0.17671002743311348)
(3.534715814303925, 5.890641491546489)
(0.611360975385955, 2.9432460366653213)
(3.94890493411447, 6.248368129219131)
(8.358501795899047, 4.655648268959565)
(3.597211873999991, 7.184515265663337)Challenge Output
(5.305665745194435,5.6162850431000875) (5.333978668303457,5.698128530439982)Bonus
A nearly 5000 point bonus set to really stress test your approach. http://hastebin.com/oyayubigof.lisp
7
u/jnazario 2 0 Sep 16 '15
here's a hastebin post of nearly 5000 points. it blew up my scala solution.
http://hastebin.com/oyayubigof.lisp
18
u/eatsfrombags Sep 16 '15 edited Sep 24 '15
Python 3.4. Recursive divide and conquer approach, O(n*log(n)) . The algorithm actually requires implementing the brute force solution as well, so I thought I'd time the difference which accounts for like half of the lines of code.
EDIT: I tried to clean this up a bit, include some of the suggestions below, include a piece of the algorithm I left out at first, and include the times for the various input files in this thread.
Feedback appreciated!
14
u/Praetorius 1 0 Sep 16 '15 edited Sep 16 '15
C++ with OpenGL rendering:
I decided to draw the points on the screen! I also drew a red line between the two points with the shortest distance; this effect didn't look as cool as I had hoped because the lines ended up being really short. I made use of two libraries to help with OpenGL: GLEW and GLFW. I also used GLM, which is a math library that makes passing multiple numbers around a little bit easier.
Screenshots:
http://i.imgur.com/8zYKQs8.jpg
http://i.imgur.com/CyDjpvW.jpg
EDIT: jnazario's input -- http://i.imgur.com/9iamWIk.jpg
With Example Input:
With Challenge Input:
Code:
32
u/OhoMinnyBlupKelsur Sep 16 '15
Python one line. Really gross. Assumes input is in grandma_input.txt. Also it prints the output with a space between the x and y in each coordinate, but I figured that it doesn't really matter. I already did enough gross things here.
5
u/[deleted] Sep 16 '15
Fortran checking in. The data format is exactly that for list-directed reads of complex numbers, so that's what I went with. This was so very easy in Fortran... I needed an easy one! thanks!
output:
4
u/wizao 1 0 Sep 16 '15 edited Sep 16 '15
Haskell:
4
u/hutsboR 3 0 Sep 16 '15
Elixir with comments:
4
u/casualfrog Sep 16 '15
JavaScript, O( n2 ) (feedback welcome):
Output:
4
u/[deleted] Sep 17 '15
Common Lisp port of the divide-and-conquer solution
1
u/frozensunshine 1 0 Sep 20 '15 edited Sep 20 '15
Link to my code. I did it using the divide and conquer method. Code works, written in C99.
/u/jnazario where is the bonus file, I can't find it.
Edit: Ran it, 5000 points case in .002 seconds.
3
u/Godspiral 3 3 Sep 16 '15 edited Sep 16 '15
In J,
something cool I learned,
though it kinda messes up the picking out the right indexes part.
cleaner extraction: (on challenge)
5.30567 5.61629
5.33398 5.69813
An adverb to simulate the very loopy process of for i 0 to 16, for j i to 16...
faster, time/space:
0.010099196768257034 238592 vs
0.0273535912468508 501248 for 2nd (cleaner and faster than original)
alternative cleaner index lifting version is actually slower, and so even though original was n2 instead of (n-1)2 / 2, most of the performance difference is in the index extraction process, which surprised me a bit.
Ooops, The right way to do this in J, is to lay out combinations:
combT =: ([: ; ([ ; [: i.@>: -~) ((1 {:: [) ,.&.> [: ,&.>/. >:&.>@:]):(0 {:: [) (<i.1 0),~ (< i.0 0) $~ -~)
6.42201 5.83321
6.62593 6.08499
3
u/JakDrako Sep 16 '15
VB.Net Solution for the 5000 points data set posted by /u/jnazario below:
I re-used the kd-tree I had for "#214 Chester the greedy Pomeranian"...
Results:
EDIT: It does the original 100 point problem in 0ms... 1872 elapsed "ticks" whatever those are.
3
u/kahless62003 Sep 16 '15 edited Sep 17 '15
Here's my C solution, comments appreciated.
edit: amended to allow for and to not blow up if you tried the
5000100000 pair data, and streamlined a bit in places and fixed the validation, and took out the atoi dependency, and made it less fussy about the file name argument. It is intended to also work if more than one batch of coordinate pairs are included in the same file and display the result of both. It'll now tell you how long it took too.3
u/cem_dp Sep 19 '15 edited Sep 19 '15
A solution in C, using a KD-tree library I'd written earlier:
https://github.com/cemeyer/dailyprogrammer-2015-09-16
Algorithm:
Solves the 100k point bonus in 250ms:
/u/lengau's 1 million and 10 million take 3.4s and 44s, respectively:
1
u/jnazario 2 0 Sep 16 '15
Scala Solution
2
u/curtmack Sep 16 '15 edited Sep 16 '15
Haskell
Pretty straightforward solution.
Note that the relative order of distances does not change if you don't calculate the square root, as square root is a strictly increasing function, so why actually do it?
Edit: Some perfectionist edits on the code.
2
u/skeeto -9 8 Sep 16 '15
C, straightforward approach O(n2). I considered building a k-d tree, but turns out it's not really needed. It can do up to 100,000 points in under a few seconds. A k-d tree, which has an efficient nearest-neighbor search, could scale well beyond that.
2
u/jeaton Sep 16 '15 edited Sep 16 '15
python 3. probably not the most efficient solution:
here it is in one line:
2
u/ullerrm Sep 16 '15
C++11. Solves the 100,000 point input in under a second.
2
u/carbonetc Sep 16 '15
JavaScript
http://jsfiddle.net/pendensproditor/wbsrsmdh/
It tests 5,000 lots in about a second. I enjoyed watching my browser catch fire attempting to test 100,000.
2
u/a_Happy_Tiny_Bunny Sep 17 '15 edited Sep 18 '15
Haskell
Using quadtrees. This is a total over-kill for this problem, but I had previously written a very simple (and dirty) quadtree implementation. Link to gist. It's not that long. I wrote that code when I was learning Haskell, so I should probably clean it up a little bit.
The Main file:
With an argument of 32, this indicates how many coordinates a leaf can hold, the output for the 100,000 long bonus is:
I'll see if I can get it under a second tomorrow.
EDIT: Link to new gist. I brought it down to >0.4s by making the quadtree strict, and by read ByteString and using an unsafe function to read doubles.
Runs a million lines in ~4.88s, and 10 millions lines in ~68s.
I don't know much about profiling Haskell yet. I think using a streaming library such as Pipes or Conduit might improve performance. The functions to insert points and find closest could also be combined. I am done for now, but I'll leave those improvements (at least to the quadtree implementation) for future challenges.
P.s. The bonus file has a different input format.
2
u/skeeto -9 8 Sep 17 '15
SQLite, ignoring the input massaging needed to get it into the table.
Solves the challenge input instantly, but really starts to slow down as more points are added. (About 1 minute for 5,000 points.)
2
u/[deleted] Sep 17 '15
More CHR in SWI-Prolog (and, as usual, I'm late to the party). It's packaged as a PrologScript.
Usage
2
u/grangelov Sep 17 '15
Perl
2
u/[deleted] Sep 17 '15 edited Sep 19 '15
let's get something straight. I am really shit at code. It's actually laughable. Here's my proof which my brain vomited out. I am not proud but I'll keep going. Python 2.7:
EDIT: on second thought.. it's working okay and I would actually if there is someone willing to provide feedback i would greatly appreciate it. On my pc the runtime on the challenge input is usually around 0.015 seconds. is that REALLY bad? I would think that initially there are a few redundant operations in the code above.
2
u/lengau Sep 17 '15 edited Sep 17 '15
My naive version. It takes under 10 seconds for 5000 points, and naturally forever for 100,000.
I'm working on a better one today.
EDIT: It should take just over 2 hours to run the 100,000 items on my machine.
2
u/wholodolo Sep 17 '15
Python brute force
2
u/robi2106 Sep 17 '15
not being able to instantly recognize this... here is a simple Q.... are these best stored as two ints split at the point in a int[] or as a double?
2
u/robi2106 Sep 17 '15
I can see I got the 100 houses sized problem correct, so I assume I also got the 100k houses size problem correct.
This was my brute force algorithm (java)
public static ArrayList<double[]> solveBruteForce(ArrayList<double[]> houses) {:I skipped calculating the actual distance and stayed with the distance squared to eliminate an extra sqrt calculation.
I've read the link others provided but I'm just having a hard time figuring out how the divide and conquer works. grrrrr
2
u/kahless62003 Sep 17 '15
Submitting another C solution in a Gist link as it is too large to post, this time using a divide and conquer algorithm, based heavily on my previous submission which had the naïve/brute force algorithm. This one processes the 100,000 pair file in about 1 and a half minutes.
Again, comments and suggestions appreciated.
As with the prior submission, it is intended to also work if more than one batch of coordinate pairs are included in the same file and display the result of both. Due to the way the array was allocated, I decided it was more trouble than it was worth to stick the array modifying code in a separate function.
2
u/fjom Sep 18 '15 edited Sep 18 '15
C# with the recurside divide and conquer algorithm. Solves the 100,000 point input in .3 seconds
1
u/[deleted] Sep 16 '15
Java. Takes txt file with point coordinates (without the number of points in the first line). Assuming correct point format.
Challenge output 2:
1
u/andriii25 Sep 16 '15 edited Sep 16 '15
Java
Pretty much bruteforce with slight optimisation (so we don't check each coordinate pair twice), but still O(n2). Runs in 8ms for the Challenge Input on my machine. Runs in 270ms for the 5000 line input.
Feedback is wanted and appreciated!
EDIT: Now the code doesn't calculate square root of distance, as that doesn't change the relative order of the distances, as pointed out by /u/curtmack
1
u/_seemethere Sep 16 '15 edited Sep 16 '15
Python 3.5 and 2.7
https://gist.github.com/seemethere/3e5aeec6343a050899f7#file-findclosest-py
1
u/JakDrako Sep 16 '15
VB.Net
Pretty easy for an intermediate problem... the bonus for the palindromes was much harder than this.
Anyway, tried to "golf" the code a bit, although VB is probably the worst choice for code golfing after COBOL.
1
u/gengisteve Sep 16 '15
Python3:
1
u/TiZ_EX1 Sep 16 '15
Haxe
I used /u/jnazario's hastebin in addition to the OP inputs. Takes a filename containing the pairs as the only argument.
The cpp target is about 50x as fast as the Neko target for this challenge!
1
u/SportingSnow21 Sep 16 '15 edited Sep 17 '15
Go
Full code here: Gist
Here's the important bits :
Edit: Lower and upper for loops are redundant with a sorted list. Removed it, halving the ops needed for the search algorithm. Bottleneck is in the data-loading, not sure how to speed that up.
I used a constricting algorithm to make the search very efficient:
Example Input:
(6.422011725438139, 5.833206713226367) (6.625930036636377, 6.084986606308885)
Distance: 0.3239996793248184
Comparisons: 71
Challenge Input:
(5.305665745194435, 5.6162850431000875) (5.333978668303457, 5.698128530439982)
Distance: 0.08660241356297696
Comparisons: 495
Benchmark with Challenge Input:
BenchmarkFullChal-8 10000 186772 ns/op 42079 B/op 623 allocs/op
1
u/gastropner Sep 16 '15 edited Sep 16 '15
So, um, this is really similar to #40 (Difficult). Tweaked it a bit from that. distance_sq returns the square of the distance, since if sqrt(a) > sqrt(b) then a > b so sqrt is unnecessary. The sorting by x is an optimisation left over from #40.
EDIT: Tried the 100 000 coordinates file; ran fast enough not to notice with answer:
C:
1
u/Eggbert345 Sep 16 '15 edited Sep 17 '15
Go brute force solution. Only took a second or two on the 5000 point list but it definitely starts to suffer on the 100,000 point list, even with the concurrency.
EDIT: Followed SportingSnow21's advice and made some changes to limit the number of iterations, as well as to avoid strangling my computer with too many goroutines. It now takes ~5 mins to process the 100,000 point list.
1
u/Rubixdoed Sep 16 '15 edited Sep 17 '15
Java
Feedback is appreciated
It is a bit strict about how the coordinates are entered.
It takes one parameter, the name of the file containing the points.
Output for challenge input:
Runs in about 28 milliseconds, almost all of which is taken up by loading the points.
Output for jnazario's 5000 points:
Runs in about 190 milliseconds, 77 of which are taken up by loading the points.
Edit: Tried out 100,000 points:
This took about 28600 milliseconds, 600 of which are taken up by loading the point.
I also had to make a slight edit to the code that loads in the points to handle the different input, because it did not have parenthesis, and was not separated by commas.
1
u/krismaz 0 1 Sep 16 '15
I remembered reading about a linear-time algorithm for this, and found this paper. The algorithm is quite easy to understand and implement.
In theory, the underlying algorithm should be O(n) expected time, but I make no guarantees for my Python 3.5 implementation.
I haven't done any specific optimizations, but it still chunks through the 100k data set in ~8sec on my machine.
1
u/Oprbaf Sep 16 '15
Go Answer:
Challenge Input:
5,000 Input with time:
100,000 Input with time:
1
u/ChazR Sep 16 '15
Naïve Haskell brute-force. I'll try a smarter algorithm if I have time later.
1
u/quickreply100 Sep 17 '15 edited Sep 17 '15
Lua
Feedback or questions welcome.
EDIT: now deals with scientific notation in co-ords (for the 5000 line file in the comments)
1
u/Godd2 Sep 17 '15 edited Sep 17 '15
Here's mine in Ruby. It runs in O(n*log(n)) due to the sort. In fact, the inner loop runs 160 times (after the break) on the challenge input.
output:
challenge output:
100k output:
100k input runs 117,211 times on the inner loop after the break. It ran 217,209 times if you include the break.
1
u/tvw Sep 17 '15
I decided to build a KDTree using Python 2.7 and Scipy's cKDTree.
Output for example input:
Runtime: 10.995 ms
Output for challenge input:
Runtime: 11.756 ms
Output for 5000 points:
Runtime: 52.873 ms
Slightly modified code for the 100,000 points:
Output for 100,000 points:
Runtime: 558.027 ms
Pretty great speed it seems. These runtimes are on my i7-2600.
1
u/codeman869 Sep 17 '15
Swift 2.0 Created a structure to hold each house position and used 2 loops to check each house (other than to itself).
1
u/ginger_rich Sep 17 '15
Python 3.4
Still a beginner but wanted to try an intermediate challenge. Feedback welcome and appreciated
1
u/NiceGuy_Ty Sep 17 '15 edited Sep 17 '15
Divide and conquer method implemented using racket. Outputs the following for the 100,000, five-thousand, and challenge input respectively, with time being in milliseconds.
Borrowed heavily from /u/eatsfrombags Python solution and from this link.
I didn't fully implement the algorithm mentioned above, as I ran into difficulty implementing the linear/brute-force method that would cut my runtime down by about 10. Nonetheless, it solves /u/JakDrako's input in around 10 seconds and /u/jnazario's in about .2, so that's enough for the night. Any feedback is highly welcome, and thanks for another fun challenge!
1
u/miracle173 Sep 17 '15 edited Sep 17 '15
Michael Rabin designed a randomized algorithm that runs in linear time. A sketch of this algorithm can be found in Rabin Flips a Coin
1
u/MEaster Sep 17 '15 edited Sep 17 '15
F#
Solution on GitHub. I think I have a bug in the divide-and-conquer implementation because when I changed the length limit to 2000 the output changes. But I can't seem to figure out where the problem is.
Results for 16:
100:
5000:
[Edit] Fixed a really stupid bug, now have correct result for the 5k:
1
u/vesche Sep 17 '15 edited Sep 17 '15
Python 2
1
u/dopple Sep 17 '15 edited Sep 17 '15
C++14. Way more verbose than I wanted it to be. Uses jscheiny/Streams library for input.
O(n) approximate solution based on the algorithm presented in https://www.cs.umd.edu/~samir/grant/cp.pdf. Inspired by /u/krismaz's python implementation.
Solves the 100k input in about 1.6s on my Macbook Air.
Here's a github link to the full code. I'll just post the important (non-boilerplate) parts here.
1
u/Sirflankalot 0 1 Sep 17 '15 edited Sep 17 '15
Python/OpenCL
I think this is the first time that something worked the first time after I got any syntax errors out. It's a weird feeling.
Built using PyOpenCL and NumPy.
Solves the challenge in a time that isn't really measurable (other then setup and imports which take < 1 sec). Solves the 5000 point solution spending 0.175 sec on the GPU, and finding the minimum in 0.02 sec. The 100,000 point solution would be too big for memory (requiring 40GB of GPU memory), and I don't have any kind of splitting mechanism implemented to allow this.
Accepts an argument of the coordinate file name.
Any feedback is appreciated.
1
u/[deleted] Sep 18 '15
Straight-forward Ruby. Has a main method for reading in the information and looping through each combination of addresses, while having separate methods for formatting the addresses correctly and for finding the distance between two addresses. The number indicating the number of lines was ignored since Ruby's file reading is so nice.
1
u/pyfan Sep 18 '15
Python 2
1
u/-drewser- Sep 18 '15
C++, brute force.
1
u/Isitar Sep 18 '15 edited Sep 18 '15
C#
I Implemented a bit of Multithreading since there was a 100 000 coord file :)
1
u/ReckoningReckoner Sep 19 '15 edited Sep 19 '15
Ruby
Brute fore one liner (ugly and slow but it works):
Better version:
1
u/NeuroXc Sep 21 '15 edited Sep 22 '15
A Rust solution using a brute force algorithm, O(C(n,2)). Runs the challenge script in 1ms, 5000 points in 74ms, 100,000 points in 27s.
1
u/smnslwl Sep 23 '15
C++ (14) brute force.
1
u/banProsper Sep 23 '15
C#, simplest way possible, takes around 4150ms to process the 5000 member list.
1
u/EvanHahn Sep 28 '15
Solution in the Wren programming language. Takes a little bit of setup to run—check out my GitHub repo for this problem.
1
u/YonkeMuffinMan Oct 01 '15
Python 2.7 I'm a little rusty so feedback is extremely welcome!
1
u/Pandajuice22 Oct 05 '15
[Ruby] A little late to the game, but seemed like an interesting problem. Used a divide and conquer algorithm, should be running at O(N log N).
I did the custom 100,000 point input posted above and it completed in less than 1s.
Code in github
Output:
1
u/DStekel Oct 09 '15
My C# solution.
1
u/Thunder_54 Dec 15 '15
Well I'm late to the party, and my solution works for the challenge input (100 points), however when I tested the data set with 5000 points, it didn't get the correct answer (based on the answers everyone else was getting). How is that possible? I chose to just brute force it. And if you're curious for the 5000 point set it took 1min 23sec to complete with the answer below:
code below, Python 3
1
u/broken_broken_ Jan 09 '16 edited Jan 13 '16
C++ solution with kd-tree. O(n log(n) ) on average, worst case O(n2 ). Runs ten million input in 20s.
https://github.com/gaultier/challenges/blob/master/232-i/grandma_house.cpp