ANEO Sponsored Puzzle discussion

I found that the inequality below is able to check whether the light (at distance ‘d’,duration ‘t’) will be green or red while moving at the velocity (‘v’).

The light will be green if:

    d/v - t [ floor(d/vt) + mod( floor(d/vt) ,2 ) ] >= 0

else:
The light will be red

  • I am actually trying to find the nearest even multiple of ‘t’ to ‘d/v’.

I suspect most of the problems people run into is this rule “You can not cross a traffic light the second it turns red !” You have to round such that this holds true in your math. That is you can hit a light just as it turns green, but you must hit a light before it turns read. Use floor and ceil wisely.

Result:
Finally, I got 100% score.

Observation:
I found that using floor(d/(v*5/18)) works very well.

Conclusion:
Thanks ANEO for this interesting puzzle. :slightly_smiling_face:

I’m annoyed by this puzzle…

/**
 * Returns time (in seconds) to travel the given distance and the given speed. 
 * @param distance (meter)
 * @param speedInkmPerHour (km/h)
 * @return time in second to travel the given distance.
 */
private int getDuration(int distance, int speedInkmPerHour);

Different implementations and result for the tests :

  • (int) Math.round(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 100% Given tests / 80% Validator tests
  • (int) Math.floor(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 90% Validator tests
  • (int) Math.ceil(distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 80% Validator tests
  • (int) (distance/ ( speedInkmPerHour/ (18d/5d) ) );
    –> 80% Given tests / 90% Validator tests

or even

  • (int) Math.round(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 100% Given tests / 80% Validator tests
  • (int) Math.floor(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 90% Validator tests
  • (int) Math.ceil(distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 80% Validator tests
  • (int) (distance/ ( speedInkmPerHour * (5d/18d) ) );
    –> 80% Given tests / 90% Validator tests

It is annoying. You must use both floor and ceil, never round. However, you haven’t said where you are using these rounding techniques. It matters. You can arrive at a light just when it turns green, however, you may not arrive at a light just as it turns red.

Thank you. I will try these.

I use the following method with the time given by the method “getDuration(int distance, int speedInKmPerHour)” :

/**
 * Returns if time is when light is green. <br/>
 * Exclude instant when green -> red <br/>
 * Include instant when red -> green.
 * @param lightPeriod (in seconds) green -> red -> green -> red
 * @param time (in seconds)
 * @return
 */
private boolean isGreen(int lightPeriod, int time)
{
	if(time < lightPeriod)
		return true; // Pass before green -> red, OK
	if(time == lightPeriod)
		return false; // Pass when green -> red, not OK
	if(time < lightPeriod*2)
		return false; // Pass before red -> green not OK
	
	return isGreen(lightPeriod, time-(lightPeriod*2));
}

But maybe is should try another approach.

IDK guys why are you going in such complicated matters on a problem which can be easily solved with a do while :))

2 Likes

Yes I don’t see why 89 is not accepted, maybe a rounding problem because with floating numbers, 24.68 is the answer

Test 2: distance 200m duration 10s with expected answer 36km/h. This would get the car at 100m in 10 second the light will go red and then the car will get to 200m at 20s when the light will be green. That is ok but what if we run at 72 or 73 or 74km/h and pass the light at the first 10 seconds? Actually you can set many velocities to pass the light since upper limit is 200km/h. In 200km/h -> 55m/s pass the light in less than 4 seconds. Discouraging

In Test 2, 200 is the distance, not the upper limit.
The upper limit is set to 50 km/h.

Now I get it. From the description I thought that Speed was always 0<speed<200 not that the upper limit was an input.
Cheers

100%, python

0.277777777777777 is helpfull

1 Like

I think neither 88 or 89 is acceptable, the answer should be an integer, and none of them work. Try to find an integer speed that passes all lights

1 Like

Remember that 0.277778 m/s is equal to 277778 * 10^-6. And 277778 is an integer, hope this hint helped you :slight_smile:

Hi there! I passed all the test cases but during the validation I can’t pass the 7th validator (German Highway) I’m using a float type only in order to convert the speed from km/h to m/s and all other comparisons and calculations are performed only with integers. I can’t understand what’s wrong… :frowning:

Am I being stupid or is the example wrong? 50km/h is 13.88889 m/s, so if I drive 200 meters at that velocity, it will take me exactly 14.4 seconds to cover that distance - meaning that the light (with a 15 seconds switching time) will still be red.

Overall not a very good puzzle, as mentioned before it doesn’t take much “skill” to calculate from km/h to m/s, but it does introduce a whole lot of new and unnecessary problems though.

“All traffic lights turn green at the same time as you enter the area.”
So the light will be green within the first 15 seconds

Hi, I’m trying my best but can’t solve it, may I have your code please.

Thanks

No, you may not.

I find already my way and solve it :stuck_out_tongue:

2 Likes