3vo

1D Snake game

3vo
Nov 19th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Snake 1D
  2. // Remember the snake game? Well, here we have a 1D variant of the game. The snake is represented by length and number of jumps (explained below). There will be a path represented by a list of integers. Each step of the path can be:
  3. // •  neutral (0) - does not change the snake size
  4. // •  growing fruit (1) - snake size grows
  5. // •  shrinking fruit (-1) - snake size shrinks
  6. // The snake will start from the first element of the path.
  7. // The snake obviously does not want to disappear so whenever a shrinking fruit is encountered if jumps are available the snake will use one of them to skip the fruit. However, if no jumps are available the snake will eat the shrinking fruit and possibly disappear.
  8. // Your task is to calculate the final size of the snake as well as the growth balance which is the sum of the remaining fruits on the path (those not consumed by the snake).
  9. // Input
  10. // Read from the standard input:
  11. // •  On the first line of input are snake size and number of jumps separated by space.
  12. // •  The second line of input, contains the path of integers (fruits), separated by a space (" ").
  13. // Output
  14. // Print to the standard output:
  15. // •  There should be 1 line of output, containing the snake size and the growth balance separated by a space.
  16. // Constraints
  17. // •  size and jumps will always be in the range of [1, 1000]
  18. // •  the path will contain only 0, 1 and -1
  19. // •  path length will be in the range of [1, 1000]
  20. // Sample Tests
  21. // Input
  22. // 3 1
  23. // 0 0 -1 -1 -1
  24. // Output
  25. // 1 -1
  26. // Input
  27. // 2 2
  28. // 1 1 1 0 1 0 1 0
  29. // Output
  30. // 7 0
  31. // Input
  32. // 3 1
  33. // -1 -1 -1 -1 0 1 1 1 1 1
  34. // Output
  35. // 0 4
  36. // Explanation:
  37. // Snake starts with size: 3 and jumps: 1
  38. // •  the first element is -1 the snake has one jump and skips it
  39. // •  next 3 elements are all -1, the snake has no jumps and eats them -> size becomes 0 (3 + (-1) + (-1) + (-1) = 0) and the snake disappears - all other fruits are left on the path
  40. // •  growth balance is (-1) + 0 +1 +1 +1 +1 +1 the first skipped fruit and the rest after snake disappearance. The sum of them all is 4
  41.  
  42.  
  43. let input = ['3 1','-1 -1 1 1 1 0 0 0']
  44.  
  45.  
  46. let print = this.print || console.log;
  47. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  48.  
  49. let sizeAndJumps=gets().split(' ');
  50. let size=Number(sizeAndJumps[0]);
  51. let jumps=Number(sizeAndJumps[1]);
  52. let path=gets().split(' ').map(Number)
  53. let growthBalance=0
  54.  
  55. for(i=0;i<path.length;i++){
  56.     if(jumps>0&&path[i]===-1){
  57.         jumps+=path[i]
  58.         size+=0
  59.     }else if(jumps>0&&size>0&&path[i]!==-1){
  60.         size+=path[i]
  61.         path[i]=0
  62.     }else if(jumps===0&&size>0){
  63.         size+=path[i]
  64.         path[i]=0
  65.     }else if(size===0){
  66.         break;
  67.     }
  68.    
  69. }
  70. // console.log(size)
  71. // console.log(path)
  72.  
  73. for(i=0;i<path.length;i++){
  74.     growthBalance+=path[i]
  75. }
  76. // console.log(growthBalance)
  77.  
  78. console.log(size+' '+growthBalance)
Add Comment
Please, Sign In to add comment