Skip to content

Instantly share code, notes, and snippets.

@shcyiza
Last active April 12, 2020 00:58
Show Gist options
  • Save shcyiza/fa8be743d7975bf31df543a00736778b to your computer and use it in GitHub Desktop.
Save shcyiza/fa8be743d7975bf31df543a00736778b to your computer and use it in GitHub Desktop.
green light opti coding game response
const speed = parseInt(readline());
const lightCount = parseInt(readline());
const lights_data = [];
const getSpeedInKmH = (dist, dur) => Math.floor(
dist / (dur /3.6)
);
for (let i = 0; i < lightCount; i++) {
var inputs = readline().split(' ');
const current = {}
current.dist = parseInt(inputs[0]);
current.dur = parseInt(inputs[1]);
lights_data.push(current);
}
function passesAtGreen(light, km_speed) {
const arrival_time_s = ((light.dist/ km_speed) * 3.6) + 0.001;
const passage_iteration = Math.floor(arrival_time_s / (light.dur));
return passage_iteration %2 === 0;
}
function computeResponse(proposal = speed, i = 0) {
if(i >= lights_data.length) {
return proposal;
}
if(passesAtGreen(lights_data[i], proposal)) {
return computeResponse(proposal, i+1);
}
return computeResponse(proposal - 1, 0);
}
console.log(computeResponse());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment