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