Advertisement
Guest User

02 Archery Tournament

a guest
Feb 26th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function solve(input = []){
  2. let fields = input.shift().split('|');
  3. let points = 0;
  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 [_, beginIndex, positions] = input[i].split('@');
  11. beginIndex = +beginIndex;
  12. positions = +positions;
  13. if(!(beginIndex < 0 || beginIndex > fields.length || positions == 0)){
  14. let index = beginIndex - positions;
  15. while(index < 0) index+=fields.length;
  16. while(index > fields.length) index -= fields.length;
  17. if(fields[index] <= 5){
  18. points+=fields[index];
  19. fields[index] = 0;
  20. }
  21. else {
  22. points+=5;
  23. fields[index] = fields[index] - 5;
  24. }
  25. }
  26. }
  27. }
  28. console.log(fields.join(' - '));
  29. console.log(`Iskren finished the archery tournament with ${points} points!`);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement