codingquest
feature image

Closest star systems

You are relaxing at a bar and strike up a conversation with a couple of other space travellers. You start talking about the vastness of space and how close (or far) different star systems are from each other. You actually get into a little bit of a disagreement because they are saying our Solar system and the Proxima Centauri system are the closest to travel between in the Milky Way at 4.247 light years. You aren't so sure and imagine that amongst all the star systems out there, there must be some that are closer to each other than that.

Determined to get to the truth of the matter, you download data for a List of Nearby Stars to extract the coordinates for stars within about 26 light years of Earth. When a system has multiple stars, you just take the coordinates of the first star in that system - your input data.

Looking at the first 5 systems in the data,

System                                  Dist         X         Y         Z
Proxima Centauri                       4.247     2.945    -3.056    -0.143
Barnard's star                         5.963     4.958     2.980     1.449
Luhman 16 A                            6.503     1.697    -6.249     0.600
WISE J085510.83-071442.5               7.532    -3.967    -5.664     2.985
Wolf 359                               7.856    -1.916    -3.938     6.522

The Dist column indicates the system distance from our solar system. The X, Y and Z columns indicate each star systems distance from sol in the x, y and z planes. To calculate the distance between any two star systems, requires finding the difference in their x/y/z distances and using those differences with Pythagoras to find the distance for that pair of star systems.

For example, to find the distance between Proxima Centauri and Barnard's star would be

distance = squareroot( square( x1-x2 ) + square( y1-y2 ) + square( z1-z2 ) )
         = squareroot( square( 2.945-4.958 ) + square( -3.056-2.980 ) + square( -0.143-1.449 ))
         = 6.559 lightyears

Example calculations for these first five systems would be:

FROM                           TO                             CALCULATED DISTANCE
Proxima Centauri               Barnard's star                 6.559
Proxima Centauri               Luhman 16 A                    3.508
Proxima Centauri               WISE J085510.83-071442.5       8.023
Proxima Centauri               Wolf 359                       8.296
Barnard's star                 Luhman 16 A                    9.825
Barnard's star                 WISE J085510.83-071442.5       12.519
Barnard's star                 Wolf 359                       10.993
Luhman 16 A                    WISE J085510.83-071442.5       6.173
Luhman 16 A                    Wolf 359                       7.312
WISE J085510.83-071442.5       Wolf 359                       4.438

Of the above limited data, the closest pair of systems is Proxima Centauri with Luhman 16 A at a distance of 3.508 light years. In this instance, 3.508 would be your answer.

Your task

Perform Pythagoras theorem in 3 dimensions on every pair of star coordinates in the input data and find the pair that are the closest distance from each other.

Give your answer to three decimal places.

Input data

Get your puzzle input data

What is the solution?