Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.

// See https://aka.ms/new-console-template for more information
using MixTest1.Models;
using System.Text.RegularExpressions;

Console.WriteLine("Hello, World!");

//Your list with coordinates
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++;
}

//var nearest = carInfos.Select(x => new GeoCoordinate(x.Latitude, x.longitude))
//                       .OrderBy(x => x.(coord))
//                       .First();


//The coord to compare
//var startPoint = new { Latitude = 1.123, Longitude = 12.3 };

//var closest = entities.Something.OrderBy(x => 12742 * SqlFunctions.Asin(SqlFunctions.SquareRoot(SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) +
//                                    SqlFunctions.Cos((SqlFunctions.Pi() / 180) * startPoint.Latitude) * SqlFunctions.Cos((SqlFunctions.Pi() / 180) * (x.Latitude)) *
//                                    SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2)))).Take(5);
Posted
Updated 18-Aug-22 23:21pm
v2
Comments
Richard MacCutchan 19-Aug-22 7:01am    
Read the instructions again: The structure of the binary data file is as follows: ...

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Start by writing code to begin reading the file: open it, close it, prove you can get to it.
Then expand on that. Open it, read the position ID, close it. Check the data you read against the file content.
Continue to expand: move the reading stuff into a method that returns a class, and fills it from the file. Make it read the first two items, and check they are right after you return it.

Continue until you have the whole of a vehicle read.

Then expand the calling code to read multiple vehicles.

Work like this, and you are expanding on working code at all times: you know that the previous stuff is solid, so all you have to do is build on that a little bit.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900