ANEO Sponsored Puzzle discussion

Hm okay… I try the other way around, so I don’t post any solutions.
Shouldn’t this code prove that 24.68 m/s get you through without getting redlights?

def proveGreenLight(predictedSpeed):
    distanceArr = [1234, 2468, 3702, 6170, 8638, 13574, 16042, 20978, 23446, 28382, 35786, 38254, 45658, 50594, 53062, 57998]
    durationArr = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
    light_count = 16

    for i in range(light_count):
        # how much time do I have to spend to get to the light
        secondsToLight = distanceArr[i]/predictedSpeed

        # calculation if it is a green or red phase
        period = secondsToLight/durationArr[i]
        if(int(period) % 2 != 0):
            return "red"

    return "green"

print(proveGreenLight(24.68)) 

-> “green”

Try if(int(period) % 2): The odd number of changes is showing that the light is red.

2 Likes

That produces the same output:
period is odd:
int(period) % 2 -> 1
int(period) % 2 != 0 -> True
period is even:
int(period) % 2 -> 0
int(period) % 2 != 0 -> False

If you can drive precisely at 88.848 km/h, so yes all lights will be green.
But are you sure your regulator is precise enough to accept a speed with decimal precision?

1 Like

Okay thats probably the point. 88 or 89 km/h will not get you through without getting redlights. Thanks guys :slight_smile:

1 Like

you arrive at light when red -> green you are okay; when green->red you arrive at light, sorry you break the traffic regulation… XD,XD,XD

Hey guys !
I did this training but i did a weird thing to succeed :
“if round(time_to_dist) - time_to_dist < 0.00001:
return round(time_to_dist)
return time_to_dist”

because there were weird values like “19.99999996”
i’m wondering if there is a better way to get rid of this ?
thanks !

Yes: use only integer speed (not duration). :smile:

1 Like

hmm sorry but i don’t get it.

I tried to :

  • do not make conversion to m/s in a function but i do all the maths in one step (so all variables are int before calculation)

I still get this weird “19.999996”… i guess i’m doing something wrong…
can i post my code here ?

To those having problems with floating point/rounding errors :
It is interesting to note that this problem can be solved without any floating point calculations.

3 Likes

It requests the integer value to pass by all lights when they are turned green. Try checking whether 88 or 89 instead of 88.848 gives you a red light somewhere.

2 Likes

I really can’t see the way you may do this? using module to keep track if it lans after ?

I could think of a solution to solve it forcing every case from 1 to the lower speed limit, someone found a way to calculate the speed without brute force?

I think the purpose of this test is to check whether you know a language well enough to recognize its weaknesses and compensate for them. I love Javascript, but it is notorious for its quirks.

In this case, the problem is floating points always lose precision and become nondeterministic, especially when dealing with conversions, which can only be deterministic by using protected fractions. Since you can’t access external libraries for this problem, you have to overcome the precision issue a different way.

Loved this challenge! Made me think twice. (^__^)

3 Likes

I can’t get the inputs to work on the ANEO challenge
The German Highway rain of traffic lights and light string all time out before the input is complete but works perfectly fine with the other challenges.
I have added the light info to collect he data and the print(len(lightinfo)) is so that I can check if the data is finished but on the 3 tasks mentioned before there is no output on all of the others it is fine. can anyone solve this for me?

speed = int(input())
light_count = int(input())
lightinfo=[]
for i in range(light_count):
distance, duration = [int(j) for j in input().split()]
lightinfo.append([distance,duration])
print(len(lightinfo))

1 Like

I pass all the test cases but when i submit, I got the 2nd and the 7th wrong.
And i can’t figure it out, can somone help me?

I don’t have hardcoded solution and I pass all given the tests, and I cant get 100% I don’t know why. Any ideas? Solutions can be only integers? (Java)
Great test by the way. I have problem at first that I did not realize the lamp can be only green and red, and not green, yellow and red. :sweat_smile:

Hello,

I also pass all test cases.
On Submit my code fails only at the 2nd validator…
Anybody an idea why? Can anybody help me and point me in the right direction?
Thanks!

After I figured out my solution the main issue I ran into was trouble with floating point math. xx.666666… rounding to xx.66668 then the next division giving xx.99999999 so then %2 is not == 0.

Wasn’t sure whether using the decimal module in python was accepted (and had no experience using that module) so I found that changing order of operations and using *10/36 instead of *1000/3600 fixed the issue.

I have the same issue… I found a workaround to resolve this problem but it is not considered as an optimized solution…