In the link below you will find a binary data file which contains a position for each of 2 million vehicles. Your task is to write a program that can find the nearest vehicle position in the data file to each of the 10 co-ordinates provided below.
In addition to being able to do this, however, your program must be able to complete all 10 lookups in less time than our benchmark. This benchmark is based on simply looping through each of the 2 million positions and keeping the closest to each given co-ordinate. This is simply repeated for each of the 10 provided co-ordinates.
The challenge set to you is to think of a more efficient way to achieve this and to implement it.
The challenge
is is in C# & .NET
Dat File and benchmark zip file:
VehiclePositions_DataFile
https://app.box.com/s/jxjj8qmokqo6oth9p8k9vtgqj7gt740z
VehiclePositions_Benchmark
https://app.box.com/s/8cxvwg4ni904yfu268cw37d5xv1gga7a
The structure of the binary data file is as follows:
PositionId = Int32
VehicleRegistration= Null Terminated ASCII String
Latitude = Float (4 byte floating-point number)
Longitude Float (4 byte floating-point number)
RecordedTimeUTC = UInt64 (number of seconds since Epoch)
The 10 co-ordinates to find the closed vehicle positions to are as follows Position 1
Latitude = 34.544909
Longitude = -102.100843
Position 2
Latitude = 32.345544
Longitude = -99.123124
Position 3
Latitude = 33.234235
Longitude = -100.214124
Position 4
Latitude = 35.195739
Longitude = -95.348899
Position 5
Latitude = 31.895839
Longitude = -97.789573
Position 6
Latitude = 32.895839
Longitude = -101.789573
Position 7
Latitude = 34.115839
Longitude = -100.225732
Position 8
Latitude = 32.335839
Longitude = -99.992232
Position 9
Latitude = 33.535339
Longitude = -94.792232
Position 10
Latitude = 32.234235
Longitude = -100.222222
What I have tried:
When I read the dat its return some weird text like this one below:
"UT\0�\bBv7��\u0015��a\0\0\0\0\u0002\0\0\0U2-100"
I seem to fail to read the dat file as a result I'm stuck and cant move on from there. I believe there is someone who worked with something like this and could actually help me go through this hell.
I just pasted the instruction in the description above and the links to the zip files.
My Code.
using MixTest1.Models;
using System.Text.RegularExpressions;
Console.WriteLine("Hello, World!");
List<CarPosition> coords = new List<CarPosition>
{
new CarPosition { position = 1, longitude = 34.544909f, Latitude = -102.100843f },
new CarPosition { position = 1, longitude = 32.345544f, Latitude = -99.123124f },
new CarPosition { position = 1, longitude = 33.234235f, Latitude = -100.214124f },
new CarPosition { position = 1, longitude = 35.195739f, Latitude = -95.348899f },
new CarPosition { position = 1, longitude = 31.895839f, Latitude = -97.789573f },
new CarPosition { position = 1, longitude = 32.895839f, Latitude = -101.789573f },
new CarPosition { position = 1, longitude = 34.115839f, Latitude =-100.225732f },
new CarPosition { position = 1, longitude = 32.335839f, Latitude = -99.992232f },
new CarPosition { position = 1, longitude = 33.535339f, Latitude = -94.792232f },
new CarPosition { position = 1, longitude = 32.234235f, Latitude = -100.222222f },
new CarPosition { position = 1, longitude = 34.544909f, Latitude = -102.100843f }
};
var coord = (dynamic)null;
coords.ForEach(x => {
coord = new GeoCoordinate(x.Latitude, x.longitude);
});
StreamReader objInput = new StreamReader(@"C:\\Users\\Downloads\\VehiclePositions_DataFile\\VehiclePositions.dat", System.Text.Encoding.Default);
string contents = objInput.ReadToEnd().Trim();
string[] split = Regex.Split(contents, "\\s+", RegexOptions.None);
int count = 1;
List<CarInfo> carInfos = new List<CarInfo>();
CarInfo c = new CarInfo();
foreach (string s in split)
{
c.positionId = Convert.ToInt32(s);
Console.WriteLine(s);
count++;
}