Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jshint esversion: 6 */
  2.  
  3. let args = [ '5 1' , '9 0 2 4 1' ];
  4.  
  5. function solve(args){
  6.     var nk = args[0].split(' ').map(Number),
  7.         numbers = args[1].split(' ').map(Number),
  8.         result = 0;
  9.         console.log(numbers);
  10. //•   each 0 - with the absolute difference of its neighboring numbers
  11. //•   all other even numbers - with the maximum of its neighboring numbers
  12. //•   each 1 - with the sum of its neighboring numbers
  13. //•   all other odd numbers - with the minimum of its neighboring numbers
  14.  
  15.  
  16.     function transform(num, left, right){
  17.         if(num === 0)
  18.         {
  19.             return Math.abs(left - right);
  20.         } else if(num % 2 === 0){
  21.             return Math.max(left, right);
  22.         } else if(num === 1){
  23.             return left + right;
  24.         }else {
  25.             return Math.min(left, right);
  26.         }
  27.     }
  28.  
  29.     let lastIndex = numbers.length
  30.  
  31.     for(let i = 0; i < nk[1]; i += 1){
  32.         let currentTransformation = [];        
  33.  
  34.         for(let j = 0; j < lastIndex; j += 1){
  35.             let nextValue;
  36.  
  37.             if(j === 0){
  38.                 nextValue = transform(numbers[j], numbers[lastIndex -1], numbers[1]);
  39.             }else if(j === numbers.length -1){
  40.                 nextValue = transform(numbers[j], numbers[j-1], numbers[0]);
  41.             }else{
  42.                 nextValue = transform(numbers[j], numbers[j-1], numbers[j + 1]);
  43.             }
  44.             currentTransformation[j] = (nextValue);
  45.         }
  46.         numbers = currentTransformation;
  47.         console.log(numbers);
  48.     }
  49.     for(let num of numbers){
  50.         result += num;
  51.     }
  52.     console.log(result);
  53.  
  54.  
  55. }
  56. solve(args);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement