Skip to content

Instantly share code, notes, and snippets.

@rene78
Created April 2, 2019 12:59
Show Gist options
  • Save rene78/597dcf66d3f41b19376bc3e292cae6ea to your computer and use it in GitHub Desktop.
Save rene78/597dcf66d3f41b19376bc3e292cae6ea to your computer and use it in GitHub Desktop.
A coding challenge regarding a turnstile
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Coding Challenge</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
/*Find the time for each person, when they can pass the turnstile
4 rules:
- If NOT used in the sec before: Preference on 1=exit
- If used as EXIT in the sec before: Preference on 1
- If used as ENTRY in the sec before: Preference on 0
- If multiple people arrive at the same time: Index is relevant
*/
let time = [0, 0, 1, 5]; //time, when they come to turnstile
let direction = [0, 1, 1, 0]; //1=exit, 0=enter
//result [2, 0, 1, 5] T0: 1, T1: 2, T2: 0, T5: 3
function getTimes(time, direction) {
let output = [];
let users = [];
let nPeoplePerTimestep = {};
let lastPersonEntered = true;
let exitOrEnter;
for (let i = 0; i < time.length; i++) {
let user = {};
user["Number"] = i;
user["Time"] = time[i];
user["InOut"] = direction[i];
users.push(user);
//Count at each time step how many people wanna pass through
if (!nPeoplePerTimestep[time[i]]) nPeoplePerTimestep[time[i]] = [];
nPeoplePerTimestep[time[i]].push(i);
}
console.log(users);
console.log(nPeoplePerTimestep);
for (let i = 0; i <= time[time.length - 1]; i++) {
//More than 1 person, who wanna go through at this timestep?
//YES!
if (nPeoplePerTimestep[i] && nPeoplePerTimestep[i].length > 1) {
console.log("Timestep " + i + ": More than 1 person!");
//First check, if the turnstile has been used in the second before
if (time[i - 1] === undefined || time[i - 1] < time[i] - 1) {
console.log("Timestep " + i + ": Turnstile has NOT been used in the second before!");
//Prefer the person who exits (1)
exitOrEnter = 1;
definePersonToGo(i, exitOrEnter);
console.log(output);
} else {
console.log("Timestep " + i + ": Turnstile HAS been used in the second before!");
//Check whether last person using the turnstile entered or left.
if (lastPersonEntered) exitOrEnter = 0;
else exitOrEnter = 1;
definePersonToGo(i, exitOrEnter);
}
}
//NO!
else if (nPeoplePerTimestep[i]) {
console.log("Timestep " + i + ": 1 Person!");
let personWhoCanGo = nPeoplePerTimestep[i][0];
output[personWhoCanGo] = i;
delete nPeoplePerTimestep[i];
}
}
function definePersonToGo(i, exitOrEnter) {
let personWhoCanGo;
for (let j = 0; j < nPeoplePerTimestep[i].length; j++) {
if (users[nPeoplePerTimestep[i][j]].InOut === exitOrEnter) {
personWhoCanGo = nPeoplePerTimestep[i][j];
break;
}
}
console.log(personWhoCanGo);
//If nobody there take the one from the other side
if (personWhoCanGo === undefined) {
personWhoCanGo = nPeoplePerTimestep[i][0];
}
//Check if person who went through entered or left
if (users[personWhoCanGo].InOut === 0) lastPersonEntered = true;
else lastPersonEntered = false;
output[personWhoCanGo] = i;
nPeoplePerTimestep[i].splice(nPeoplePerTimestep[i].indexOf(personWhoCanGo), 1) //Remove person from nPeoplePerTime array
// console.log(nPeoplePerTimestep);
//Add all remaining people from this timestep to the next one
if (!nPeoplePerTimestep[i + 1]) nPeoplePerTimestep[i + 1] = [];
nPeoplePerTimestep[i + 1].push(...nPeoplePerTimestep[i]);
//Delete this timestep from nPeoplePerTime
delete nPeoplePerTimestep[i];
}
console.log(output);
}
getTimes(time, direction);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment