Advertisement
tetris555

world-tour

Nov 20th, 2022
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let stops = arr.shift();
  3.     arr.pop();
  4.     let temp = [];
  5.  
  6.     const reAddStop = /Add Stop:\s*(?<idx>\d+)\s*:\s*(?<string>\w+)/;
  7.     const reRemoveStop = /Remove Stop:\s*(?<start>\d+)\s*:\s*(?<end>\d+)/;
  8.     const reSwitch = /Switch:\s*(?<fromPlace>\w+)\s*:\s*(?<toPlace>\w+)/;
  9.  
  10.     for (const line of arr) {
  11.         if (reAddStop.test(line)) {
  12.             let {idx, string} = line.match(reAddStop).groups;
  13.             idx = Number(idx);
  14.  
  15.             if (stops.charAt(idx)) {
  16.                 temp = [...stops];
  17.                 temp.splice(idx, 0, string);
  18.                 stops = temp.join('');
  19.             }
  20.             console.log(stops);
  21.         }
  22.         else if (reRemoveStop.test(line)) {
  23.             let {start, end} = line.match(reRemoveStop).groups;
  24.             start = Number(start);
  25.             end = Number(end);
  26.  
  27.             if (stops.charAt(start) && stops.charAt(end)) {
  28.                 temp = [...stops] ;
  29.                 temp.splice(start, (end - start + 1));
  30.                 stops = temp.join('');
  31.             }
  32.             console.log(stops);
  33.         }
  34.         else if (reSwitch.test(line)) {
  35.             const {fromPlace, toPlace} = line.match(reSwitch).groups;
  36.  
  37.             if (stops.includes(fromPlace)) {
  38.                 stops = stops.replace(new RegExp(`${fromPlace}`), () => toPlace);
  39.             }
  40.             console.log(stops);
  41.         }
  42.     }
  43.     console.log(`Ready for world tour! Planned stops: ${stops}`);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement