Advertisement
Iv555

Untitled

Apr 20th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. function solve(input) {
  2. const initialPositions = input.shift()
  3. const horsesInit = initialPositions.split('|');
  4.  
  5. let horses = [];
  6. for (let index = horsesInit.length - 1; index >= 0; index--) {
  7. const horse = horsesInit[index];
  8. horses.push(horse)
  9. }
  10. for (const item of input) {
  11. if (item === 'Finish') {
  12. break;
  13. }
  14.  
  15. if (item.startsWith('Retake')) {
  16. const [_retake, overtakingHorse, overtakenHorse] = item.split(' ');
  17. const indexOvertakingHorse = horses.indexOf(overtakingHorse);
  18. const indexOvertakenHorse = horses.indexOf(overtakenHorse);
  19. if (indexOvertakingHorse > indexOvertakenHorse) {
  20. const temp = horses[indexOvertakingHorse]
  21. horses[indexOvertakingHorse] = horses[indexOvertakenHorse];
  22. horses[indexOvertakenHorse] = temp;
  23. console.log(`${overtakingHorse} retakes ${overtakenHorse}.`)
  24. }
  25. //console.log(horses.join(' '))
  26.  
  27. }
  28. else if (item.startsWith('Trouble')) {
  29. const [_trouble, horseName] = item.split(' ');
  30. let indexHorse = horses.indexOf(horseName)
  31. if (indexHorse != horses.length - 1) {
  32.  
  33. const temp = horses[indexHorse + 1];
  34. horses[indexHorse + 1] = horses[indexHorse];
  35. horses[indexHorse] = temp;
  36. console.log(`Trouble for ${horseName} - drops one position.`)
  37. }
  38. //console.log(horses.join(' '))
  39.  
  40. }
  41. else if (item.startsWith('Rage')) {
  42. const [_rage, horseName] = item.split(' ');
  43. let indexHorse = horses.indexOf(horseName);
  44. if (indexHorse === 1) {
  45. const temp = horses[1];
  46. horses[1] = horses[0];
  47. horses[0] = temp;
  48. }
  49. else if (indexHorse >= 2) {
  50. horses.splice(indexHorse, 1);
  51. horses.splice(indexHorse - 2, 0, horseName)
  52. // const temp = horses[indexHorse];
  53. // horses[indexHorse] = horses[indexHorse - 2];
  54. // horses[indexHorse - 2] = temp;
  55. }
  56. console.log(`${horseName} rages 2 positions ahead.`)
  57. //console.log(horses.join(' '))
  58. }
  59. else if (item === 'Miracle') {
  60. const lastHorse = horses.pop();
  61. horses.unshift(lastHorse)
  62. console.log(`What a miracle - ${lastHorse} becomes first.`);
  63. //console.log(horses.join(' '))
  64. }
  65. }
  66. console.log(horses.reverse().join('->'))
  67. console.log(`The winner is: ${horses[horses.length-1]}`)
  68. }
  69.  
  70. // solve(['Bella|Alexia|Sugar',
  71. // 'Retake Alexia Sugar',
  72. // 'Rage Bella',
  73. // 'Trouble Bella',
  74. // 'Finish'])
  75.  
  76.  
  77. // solve(['Onyx|Domino|Sugar|Fiona',
  78. // 'Trouble Onyx',
  79. // 'Retake Onyx Sugar',
  80. // 'Rage Domino',
  81. // 'Miracle',
  82. // 'Finish'])
  83.  
  84. solve(['Fancy|Lilly',
  85. 'Retake Lilly Fancy',
  86. 'Trouble Lilly',
  87. 'Trouble Lilly',
  88. 'Finish',
  89. 'Rage Lilly'])
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement