Advertisement
Guest User

Move the matrix

a guest
Mar 7th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Impl.
  2. var p=parseInt;
  3. function m(d, i, l, c) {
  4.     if (c>=l*l) return i;
  5.     var x=[d<5?c%l:l-1-c%l,d<5?l-1-p(c/l):p(c/l),i[p(c/l)][c%l],m(d,i,l,c+1)];
  6.     x[3][x[0]][x[1]]=x[2];
  7.     return x[3];
  8. }
  9.  
  10. // Demo
  11. print(                          // Pretty print matrix
  12.     m(                      // "Move"-function
  13.         0,                  // Zero-based day of week (0 = monday ..)
  14.         [                   // Input matrix
  15.             [ 1, 2, 3, 4, 5, 6],
  16.             [ 7, 8, 9,10,11,12],
  17.             [13,14,15,16,17,18],
  18.             [19,20,21,22,23,24],
  19.             [25,26,27,28,29,30],
  20.             [31,32,33,34,35,36]
  21.         ],
  22.         6,                  // N size of matrix
  23.         0                   // Counter, init at 0
  24.     )
  25. );
  26.  
  27. // Pretty print
  28. function print(arr) {
  29.     for (var i = 0; i < arr.length; i++) {
  30.         console.log('');
  31.         for (var j = 0; j < arr[i].length; j++)
  32.             process.stdout.write(" "+arr[i][j]);
  33.     }
  34.     console.log('');
  35.     console.log('');
  36. }
  37.  
  38. // Output
  39. // 31 25 19 13 7 1
  40. // 32 26 20 14 8 2
  41. // 33 27 21 15 9 3
  42. // 34 28 22 16 10 4
  43. // 35 29 23 17 11 5
  44. // 36 30 24 18 12 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement