Created
September 24, 2014 22:30
-
-
Save G33kDude/e7b093b80fc9fd2b7fc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NoEnv | |
SetBatchLines, -1 | |
Conversion := {"mph": 0.44704, "km/s": 0.277778} | |
MsgBox, Put the input on the clipboard then hit OK | |
Data := ParseInput(Clipboard) | |
Data.Cameras.Remove(1) ; This is a redundant camera for my purposes | |
for RegNumber, Times in Data.Cars | |
{ | |
Speeds := [] | |
LastEntry := ToSeconds(Times.Remove(1)) | |
for Index, Time in Times | |
{ | |
Seconds := ToSeconds(Time) | |
MetersPerSecond := Data.Cameras[Index] / (Seconds - LastEntry) | |
Speeds[Time] := MetersPerSecond / Conversion[Data.Unit] | |
LastEntry := Seconds | |
} | |
for Time, Speed in Speeds | |
if (Speed > Data.Limit) | |
Out .= "Vehicle " RegNumber " broke the speed limit by " Speed - Data.Limit " " Data.Unit " at " Time ".`n" | |
} | |
MsgBox, % Out | |
return | |
ParseInput(Input) | |
{ | |
static LimitRE := "(?<Limit>\S+)\s(?<Unit>km/h|mph)" | |
, CameraRE := "(?<Number>\d+) is (?<Distance>\S+)" | |
, LogRE := "camera (?<Number>\d+).$" | |
, VehicleRE := "^Vehicle (?<RegNumber>\S+\s+\S+) passed camera (?<Number>\d+) at (?<Time>\S+)\.$" | |
Cameras := [] | |
Cars := [] | |
LastDist := 0 | |
for each, Line in StrSplit(Input, "`n", "`r") | |
{ | |
if !(Line := Trim(Line)) | |
continue ; blank line | |
if RegExMatch(Line, VehicleRE, Match) | |
{ ; Vehicle log line | |
if !Cars.HasKey(MatchRegNumber) | |
Cars[MatchRegNumber] := [] | |
Cars[MatchRegNumber, MatchNumber] := MatchTime | |
} | |
else if RegExMatch(Line, CameraRE, Match) | |
{ ; Camera definition line | |
Cameras[MatchNumber] := MatchDistance - LastDist | |
LastDist := MatchDistance | |
} | |
else if RegExMatch(Line, LogRE, Match) | |
{ ; Camera log section | |
CurrentCamera := MatchNumber | |
} | |
else if RegexMatch(Line, LimitRE, Match) | |
{ ; Speed limit definition line | |
Limit := MatchLimit | |
Unit := MatchUnit | |
} | |
else | |
throw Exception("Invalid line" Line) | |
} | |
return {"Limit": Limit, "Unit": Unit, "Cameras": Cameras, "Cars": Cars} | |
} | |
ToSeconds(Time) | |
{ | |
x := StrSplit(Time, ":") | |
return x[1]*60*60 + x[2]*60 + x[3] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment