Advertisement
ErolKZ

Untitled

Dec 7th, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let fieldSize = input.shift();
  5.  
  6. let indexes = input.shift().split(' ').map(el => Number(el));
  7.  
  8. let field = [];
  9.  
  10. field = field.slice(0, fieldSize);
  11.  
  12. if (field.length < fieldSize) {
  13.  
  14. let addField = fieldSize - field.length;
  15.  
  16. for (let i = 0; i < addField; i++) {
  17.  
  18. if (!indexes.includes(i)) {
  19.  
  20. field.push(0);
  21.  
  22. } else {
  23.  
  24. field.push(1);
  25.  
  26. }
  27.  
  28. }
  29.  
  30. }
  31.  
  32. // console.log(field);
  33.  
  34.  
  35. for (let el of input) {
  36.  
  37. let cur = el.split(' ');
  38.  
  39. let startIndex = Number(cur[0]);
  40.  
  41. let direction = cur[1];
  42.  
  43. let flyLength = Number(cur[2]);
  44.  
  45. let landingIndex = startIndex + flyLength;
  46.  
  47. if (direction === 'left') {
  48.  
  49. landingIndex = startIndex - flyLength;
  50.  
  51. }
  52.  
  53. if (direction === 'right') {
  54.  
  55. while (field[landingIndex] !== 0 && field[landingIndex] !== undefined) {
  56.  
  57. landingIndex++;
  58.  
  59. }
  60.  
  61. if (field[landingIndex] !== undefined) {
  62.  
  63. field[landingIndex] = 1;
  64.  
  65. field[startIndex] = 0;
  66.  
  67. startIndex = landingIndex;
  68.  
  69. } else {
  70.  
  71. field[startIndex] = 0;
  72.  
  73. }
  74.  
  75. } else if (direction === 'left') {
  76.  
  77. while (field[landingIndex] !== 0 && field[landingIndex] !== undefined) {
  78.  
  79. landingIndex--;
  80.  
  81. }
  82.  
  83. if (field[landingIndex] !== undefined) {
  84.  
  85. field[landingIndex] = 1;
  86.  
  87. field[startIndex] = 0;
  88.  
  89. startIndex = landingIndex;
  90.  
  91. } else {
  92.  
  93. field[startIndex] = 0;
  94.  
  95. }
  96.  
  97. }
  98.  
  99. }
  100.  
  101. console.log(field.join(' '));
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement