Your mission, should you choose to accept it, is to write code for a GPS receiver.
- The current time, as nanoseconds from the Unix epoch. [EDIT: This is optional, please state whether you require it]
- Four satellite signals, in the following format:
- The time the signal was sent, as nanoseconds from the Unix epoch. You must be able to handle dates up to and including 2020.
- The location of the satellite, as Cartesian coordinates, in metres. You must be able to handle values that fit into a signed 32-bit integer (-2,147,483,648 to 2,147,483,647). Only integer coordinates will be given. You may assume valid input (i.e. your position can be calculated)
Input can be provided from command-line arguments, standard input or equivalent, but not from a variable. The input can have any separator characters in it; please specify what your input format is.
The coordinates of your receiver, to the nearest 1000 metres, using 299,792,458 m/s as the speed of light. Again, I will be lenient with the output format: any separators are acceptable.
1412349052664203400 [1412349052692915310,2267943, 13318342, 0] [1412349052698278110,-3757960, 3500627, 0] [1412349052691548521,4425976, -1533193, 3469445] [1412349052687888295,10622179, 11246951, 84184]
(6223615, 5673496, 0)
How GPS Works
Because the speed of light is finite, there will be a difference between your current time and the time the satellite sent the signal.
- Use the difference to calculate the distance to the satellite, d.
- You are clearly located somewhere on the surface of a sphere, centered on the satellite, with radius d.
- Once you have two satellites and two spheres, you have to be located somewhere on the intersection of the two sphere surfaces, which is a circle.
- Adding another satellite reduces the possible positions to just two points, where all three sphere surfaces intersect.
- The fourth satellite allows you to decide on a specific one of these points. Your task is to calculate the location of this point, where all four spheres intersect. Since you assume valid input, such a point does exist.
- Standard loopholes are banned.