Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Snake 1D
- // 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:
- // • neutral (0) - does not change the snake size
- // • growing fruit (1) - snake size grows
- // • shrinking fruit (-1) - snake size shrinks
- // The snake will start from the first element of the path.
- // 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.
- // 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).
- // Input
- // Read from the standard input:
- // • On the first line of input are snake size and number of jumps separated by space.
- // • The second line of input, contains the path of integers (fruits), separated by a space (" ").
- // Output
- // Print to the standard output:
- // • There should be 1 line of output, containing the snake size and the growth balance separated by a space.
- // Constraints
- // • size and jumps will always be in the range of [1, 1000]
- // • the path will contain only 0, 1 and -1
- // • path length will be in the range of [1, 1000]
- // Sample Tests
- // Input
- // 3 1
- // 0 0 -1 -1 -1
- // Output
- // 1 -1
- // Input
- // 2 2
- // 1 1 1 0 1 0 1 0
- // Output
- // 7 0
- // Input
- // 3 1
- // -1 -1 -1 -1 0 1 1 1 1 1
- // Output
- // 0 4
- // Explanation:
- // Snake starts with size: 3 and jumps: 1
- // • the first element is -1 the snake has one jump and skips it
- // • 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
- // • 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
- let input = ['3 1','-1 -1 1 1 1 0 0 0']
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let sizeAndJumps=gets().split(' ');
- let size=Number(sizeAndJumps[0]);
- let jumps=Number(sizeAndJumps[1]);
- let path=gets().split(' ').map(Number)
- let growthBalance=0
- for(i=0;i<path.length;i++){
- if(jumps>0&&path[i]===-1){
- jumps+=path[i]
- size+=0
- }else if(jumps>0&&size>0&&path[i]!==-1){
- size+=path[i]
- path[i]=0
- }else if(jumps===0&&size>0){
- size+=path[i]
- path[i]=0
- }else if(size===0){
- break;
- }
- }
- // console.log(size)
- // console.log(path)
- for(i=0;i<path.length;i++){
- growthBalance+=path[i]
- }
- // console.log(growthBalance)
- console.log(size+' '+growthBalance)
Add Comment
Please, Sign In to add comment