Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let race = {};
- input.forEach(line => {
- let [command, road, racer, nextRoad] = line.split('->');
- // console.log(command);
- switch(command){
- case "Add":
- if(!race.hasOwnProperty(road)){
- race[road] = [];
- race[road].push(racer);
- } else if(!race[road].includes(racer)){
- race[road].push(racer);
- }
- // console.log(race);
- break;
- case "Move":
- if(race.hasOwnProperty(road)){
- let index = race[road].indexOf(racer);
- race[road].splice(index, 1);
- race[nextRoad].push(racer);
- }
- // console.log(race);
- break;
- case "Close":
- delete race[road];
- // console.log(race);
- break;
- case "END":
- break;
- }
- });
- let sorted = Object.entries(race).sort((a,b) => b[1].length - a[1].length || a[0].localeCompare(b[0]));
- console.log("Practice sessions:");
- for(let kvp of sorted){
- console.log(`${kvp[0]}`);
- for(let kkk of kvp[1]){
- console.log(`++${kkk}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement