Advertisement
Guest User

Archery Tournament 02 Mid

a guest
Feb 27th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. function solve(input = []){
  2. let points = 0;
  3. let fields = Array.from(input.shift().split('|'), Number);
  4. for(let i = 0; i < input.length; i++){
  5. if(input[i] == 'Reverse')
  6. fields = fields.reverse();
  7. else if(input[i] == 'Game over')
  8. break;
  9. else {
  10. let [command, beginIndex, positions] = input[i].split('@');
  11. beginIndex = +beginIndex;
  12. positions = +positions;
  13. if(beginIndex >= 0 && beginIndex < fields.length){
  14. let index;
  15. if(command.includes('Left') && positions > 0 ||
  16. command.includes('Right') && positions < 0){ // moves left
  17. index = beginIndex - Math.abs(positions);
  18. while(index < 0) index+=fields.length;
  19. //console.log(index);
  20. }
  21. else{ // moves right
  22. index = beginIndex + Math.abs(positions);
  23. while(index > fields.length) index-=fields.length;
  24. //console.log(index);
  25. }
  26. if(fields[index] > 5){
  27. fields[index] -= 5;
  28. points += 5;
  29. }
  30. else{
  31. points += fields[index];
  32. fields[index] = 0;
  33. }
  34. }
  35. }
  36. }
  37. console.log(fields.join(' - '));
  38. console.log(`Iskren finished the archery tournament with ${points} points!`);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement