r/dailyprogrammer • u/Elite6809 1 1 • Sep 24 '14
[09/24/2014] Challenge #181 [Intermediate] Average Speed Cameras
(Intermediate): Average Speed Cameras
In the UK, a common safety measure on motorways is the so-called average speed cameras. These, unlike normal speed cameras which measure a vehicle's speed instantaneously, have several connected cameras at intervals along a motorway. The speed of a vehicle can be determined by dividing the distance between two cameras by the time it takes the vehicle to get from one to another. This can be used to stop vehicles breaking the speed limit over long stretches of roads, rather than allowing vehicles to speed up after they are out of range. The Home Office has contacted you to replace the aging software system in the cameras with something more up to date.
In this challenge, you will be given a number of speed cameras and their positions along a road, along with the speed limit. You will then be given the camera logs for each camera in turn. From this data, you will work out which vehicles are breaking the speed limit.
Formal Inputs and Outputs
Input Description
The first section of the input will contain the speed limit and the position of the speed cameras. The speed limit may be in miles per hour or kilometres per hour. The lines will be in the format:
Speed limit is <limit> mph.OR
Speed limit is <limit> km/h.The lines describing the positions of the speed cameras will look like:
Speed camera <number> is <distance> metres down the motorway.Speed camera number 1 will always have a distance of 0.
After this, you will get logs for each speed camera, like this:
Start of log for camera <number>:
Vehicle <registration number> passed camera <number> at <time>.
Vehicle <registration number> passed camera <number> at <time>.
...Example inputs and outputs can be found below.
Output Description
For each vehicle that breaks the speed limit, print a line like so:
Vehicle <registration number> broke the speed limit by <amount>.Where <amount> is in the local units.
Sample Inputs and Outputs
Sample Input
Speed limit is 60.00 mph.
Speed camera number 1 is 0 metres down the motorway.
Speed camera number 2 is 600 metres down the motorway.
Speed camera number 3 is 855 metres down the motorway.
Speed camera number 4 is 1355 metres down the motorway.
Start of log for camera 1.
Vehicle G122 IVL passed camera 1 at 09:36:12.
Vehicle H151 KEE passed camera 1 at 09:36:15.
Vehicle U109 FIJ passed camera 1 at 09:36:20.
Vehicle LO04 CHZ passed camera 1 at 09:36:23.
Vehicle I105 AEV passed camera 1 at 09:36:28.
Vehicle J828 EBC passed camera 1 at 09:36:29.
Vehicle WF EP7 passed camera 1 at 09:36:32.
Vehicle H108 KYL passed camera 1 at 09:36:33.
Vehicle R815 FII passed camera 1 at 09:36:34.
Vehicle QW04 SQU passed camera 1 at 09:36:34.
Start of log for camera 2.
Vehicle G122 IVL passed camera 2 at 09:36:42.
Vehicle LO04 CHZ passed camera 2 at 09:36:46.
Vehicle H151 KEE passed camera 2 at 09:36:51.
Vehicle QW04 SQU passed camera 2 at 09:36:53.
Vehicle J828 EBC passed camera 2 at 09:36:53.
Vehicle R815 FII passed camera 2 at 09:36:55.
Vehicle U109 FIJ passed camera 2 at 09:36:56.
Vehicle H108 KYL passed camera 2 at 09:36:57.
Vehicle I105 AEV passed camera 2 at 09:37:05.
Vehicle WF EP7 passed camera 2 at 09:37:10.
Start of log for camera 3.
Vehicle LO04 CHZ passed camera 3 at 09:36:55.
Vehicle G122 IVL passed camera 3 at 09:36:56.
Vehicle H151 KEE passed camera 3 at 09:37:03.
Vehicle QW04 SQU passed camera 3 at 09:37:03.
Vehicle J828 EBC passed camera 3 at 09:37:04.
Vehicle R815 FII passed camera 3 at 09:37:09.
Vehicle U109 FIJ passed camera 3 at 09:37:11.
Vehicle H108 KYL passed camera 3 at 09:37:12.
Vehicle I105 AEV passed camera 3 at 09:37:20.
Vehicle WF EP7 passed camera 3 at 09:37:23.
Start of log for camera 4.
Vehicle LO04 CHZ passed camera 4 at 09:37:13.
Vehicle QW04 SQU passed camera 4 at 09:37:24.
Vehicle J828 EBC passed camera 4 at 09:37:26.
Vehicle G122 IVL passed camera 4 at 09:37:28.
Vehicle R815 FII passed camera 4 at 09:37:28.
Vehicle H151 KEE passed camera 4 at 09:37:29.
Vehicle H108 KYL passed camera 4 at 09:37:36.
Vehicle I105 AEV passed camera 4 at 09:37:42.
Vehicle WF EP7 passed camera 4 at 09:37:44.
Vehicle U109 FIJ passed camera 4 at 09:37:45.Sample Output
Vehicle LO04 CHZ broke the speed limit by 3.4 mph.
Vehicle LO04 CHZ broke the speed limit by 2.1 mph.
Vehicle QW04 SQU broke the speed limit by 10.6 mph.
Vehicle R815 FII broke the speed limit by 3.9 mph.Challenge
Challenge Input
A long pastebin containing a huge data set is available here, to stress-test your input if nothing else.
Notes
You may want to use regular expressions again for this challenge.
6
u/G33kDude 1 1 Sep 24 '14 edited Sep 25 '14
I'm getting consistently different data than you for some reason. On average, I have two people going over the limit. When examining each section individually, I see three people going over the limit.
http://i.imgur.com/Sg76Eu0.png
Edit: "Solved" according to my code (In AutoHotkey)
https://gist.github.com/G33kDude/e7b093b80fc9fd2b7fc8
Output:
Edit: Figured I may as well paste the challenge output.
Not guaranteed to be correct https://dl.dropbox.com/s/f4eiiwca42mfolx/2014-09-24_22-08-40.txt
2
u/dongas420 Sep 25 '14 edited Sep 25 '14
There's something seriously wrong with your challenge input/output, OP: there are cars in the output that don't even exist in the input.
Perl:
4
u/Vectorious Sep 25 '14 edited Sep 18 '25
skirt growth school spectacular sparkle humorous plant society head teeny
This post was mass deleted and anonymized with Redact
1
u/dohaqatar7 1 1 Sep 24 '14
Two questions:
Will vehicles pass the cameras in the correct order?
Will all entries in camera logs take place on the same day?
2
u/XenophonOfAthens 2 1 Sep 24 '14 edited Sep 24 '14
This was a fun little problem!
So, my output looks a little bit different from what's supposed to happen. This is my output:
(edit: i rounded off the values)
Now, as you can see, I find that there are three cars that break the speed limit for the example case. I talked with /u/G33kDude on IRC, and he also got the same three cars with the same values, so either we're both wrong, or the problem is wrong. I don't know which.
Also, you may ask, "hey, there's only 3 stretches of road, how can one of the cars break the speed limit 4 times?". I'm glad you asked! The reason is that the way I wrote the prolog code is that I asked for the log entries for each car for each possible pair of cameras. That means I'm not just measuring speed between cameras 1 and 2, 2 and 3, and 3 and 4. I'm measuring all combinations: cameras (1,2),(1,3),(1,4),(2,3),(2,4) and (3,4). The reason I'm doing this is that this is the natural query you run in Prolog, it would have been harder to write it the other way (edit: not really harder, but less natural maybe). This has the downside of making the output for the longer given example like a gazillion lines long, so I'm not gonna post it.
Anyway, here's my code:
2
u/skeeto -9 8 Sep 25 '14
C. I'm not getting answers matching the provided output, but it matches /u/dongas420's output. I'm assuming the events for any particular car input in chronological order. I also don't worry about crossing the midnight boundary in a single trip, since the input is ambiguous about the date anyway.
2
u/fbgm1337 Sep 25 '14 edited Sep 25 '14
JAVA. Here is my solution! Took me a while to write this, but I think i have it correct.
I am not getting all of the same outputs as everyone else.Here are my outputs:Here is my code.
EDIT: Updated my outputs. Now I am getting the same outputs as others.
2
u/addaone Sep 25 '14
Ruby. I'm getting the same results as /u/G33kDude.
https://github.com/snickroger/dailyprogrammer/blob/master/ruby/181i.rb
2
u/hutsboR 3 0 Sep 25 '14 edited Sep 25 '14
Dart: (AGAIN)
Rewrote the entire solution, made it more beautiful. 1000x faster too.
Output:
Challenge output - I did some comparisons with /u/G33kDude's challenge output and it seems like we've got the same data, for the most part. He has a bit more vehicles.
2
u/chunes 1 2 Sep 26 '14
I've never felt so dirty by considering one of these problems before. I don't want to work for big brother!
2
u/ddsnowboard Sep 27 '14
Python 3.4. That was a good one; I know I'm really late to the party, but I just did it about 2 hours ago, and I wanted to post it. Advice appreciated.
EDIT: Some of the lines are really long and hard to read. Here's a github link if you're interested.
1
u/Frigguggi 0 1 Sep 25 '14
Java:
Output:
1
u/thegoo280 Sep 25 '14 edited Sep 25 '14
I am also getting similar output as the others. I uglied up my code as I did not notice until completion that we were going to print the output in mph, but no matter!
I tried to make my RegEx a little more durable than probably necessary in the idea that maybe the output format could change in the future. Also I like that I print the speed violations in order of occurrance (far as I can tell)
Python 3 :
Output:
1
u/dohaqatar7 1 1 Sep 25 '14
I'm getting all the right vehicles for the output, but I'm off on by how much they were speeding.
Java
1
u/mtlife Sep 25 '14
PHP
Accepts 3 arguments, so it can calculate the original average of average and the new format. File input can be specified.
Sample output:
Script:
1
u/Befriendswbob Sep 25 '14
Here is my solution in C#.
Sample Output:
1
u/dunnowins Sep 25 '14
A very ugly Ruby solution. I tried to write it as quickly as possible sooooo it's not nice but it works.
Edit: Added ouput
OUTPUT:
1
u/OllieShadbolt 1 0 Sep 25 '14
Python 3.2.5
1
u/rectal_smasher_2000 1 1 Sep 25 '14 edited Sep 26 '14
c++ (msvc2012)
NOTE: this is quite a basic solution without any error checking, so it relies on the data being provided in a correct order. say, if you were to shuffle camera positions so that camera 1 at 0 metres came after camera 2 at 600, bad things would happen. also, didn't seem to read the challenge properly, so my solution only supports mph.
1
u/MuffinsLovesYou 0 1 Sep 25 '14 edited Sep 25 '14
Answer in SQL (because I'm lazy)
/* Output (just tested with 3, one a non-speeder) Vehicle LO04 CHZ broke the speed limit by 10.6
Vehicle QW04 SQU broke the speed limit by 3.4
Vehicle QW04 SQU broke the speed limit by 2.1
*/
1
u/jeaton Sep 25 '14
C11:
Output:
Vehicle QW04 SQU broke the speed limit by 10.640093 mph.
Vehicle R815 FII broke the speed limit by 3.912465 mph.
Vehicle LO04 CHZ broke the speed limit by 3.379862 mph.
Vehicle LO04 CHZ broke the speed limit by 2.137119 mph.
1
u/cooper6581 Sep 26 '14
Go.
This was a big pain compared to doing the same exercise in Python
1
u/SilentBunny Sep 26 '14
I had a headache doing conversions :( Oh well, here it is in Ruby
Code: https://gist.github.com/AlessandroMinali/55344a9ece5a961e026a
Output:
1
u/minikomi Sep 26 '14
I decided to try and make a d3.js chart out of this challenge. Mouse over the rows in the table to highlight one of the vehicles. It works with the Challenge input - but it's pretty heavy for the browser to draw!
http://bl.ocks.org/minikomi/raw/65c575f14ae6d9c44b2e/
Source is.. pretty long. I've put it in a gist:
https://gist.github.com/minikomi/65c575f14ae6d9c44b2e
Edit: oops.. didn't label my axes! y is time, x is meters.
1
u/Jam0864 Sep 26 '14
Bit late fo the party, but here's a solution in Scala. A few little niggly bits, but overall pretty happy with it.
sealed trait LogLine case class SpeedLimit(limit: Double, units: String) extends LogLine case class Camera(camNum: Int, distance: Int) extends LogLine case class Image(numPlate: String, camNum: Int, time: String) extends LogLine
1
u/papabalyo Sep 26 '14
Clojure: https://gist.github.com/kishanov/a1dc043ce1dd1c37740f Gist also includes processed output for both test cases
1
u/Busybyeski Sep 26 '14
Python 2
My output only reports one occurrence of speeding for each vehicle: their most severe. It also sorts the list from least to most severe so the biggest speeders can be seen grouped together. I used regex for some practice.
https://github.com/blakebye/dailyprogrammer/blob/master/181/medium%20-%20average%20speed%20cameras.py
1
u/viciu88 Sep 26 '14
Java 1.7
Had fun with named regex groups
1
u/undergroundmonorail Sep 26 '14
Are we supposed to detect if they ever went over the speed limit, or if they just averaged over it? You can go really fast from camera 1 to camera 2, then go slow on your way to camera 3 and be below the limit overall. What should we print here?
It appears that the answer is "only if they average over it" but then it seems like you could
which is pretty exploit-y. I'm going to go over the test data again and see if I missed anything.
1
u/Ratheronfire Sep 27 '14 edited Sep 28 '14
I've been learning a bit of Python for my classes, so I decided to use it for this challenge:
Output:
1
u/fgsguedes 0 0 Sep 29 '14
Java 8 solution.
Output:
1
u/VerifiedMyEmail Oct 01 '14
Python 3.3
This only works for mph. I am happy with how small my functions are. I think it is hot.
1
u/DasEwigeLicht Oct 01 '14
Good old Java 8 - lambdas, parallel streams and a bit of the new DateTime API:
As always comments are welcome.
1
u/[deleted] Oct 04 '14
This is great!
1
u/DasEwigeLicht Oct 12 '14
After Java I did some Python as well, which imo turned out quite nicely:
1
u/no_wallpaper Dec 15 '14
I like this!!
1
u/PapaJohnX Dec 17 '14 edited Dec 17 '14
I know this is an old challenge, but I had it bookmarked and thought it would be a good exercise for learning Python. So:
Python 3
Output -