Advertisement
simeonshopov

Rosetta Stone

Mar 24th, 2021
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   const rosetta = {
  3.     0: ' ', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M',
  4.     14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z',
  5.   };
  6.  
  7.   const matrixSize = Number(arr.shift());
  8.   const template = createMatrix(matrixSize);
  9.   let matrix = [...arr].map(x => x.split(' ').map(Number));
  10.   let i = 0;
  11.   let j = 0;
  12.  
  13.   while (i < matrix.length && j < matrix.length) {
  14.     for (let x = 0; x < template.length; x++) {
  15.       for (let y = 0; y < template[x].length; y++) {
  16.         const templateCell = template[x][y];
  17.         if (doesExist(i + x) && doesExist(j + y)) {
  18.           let matrixCell = rotate(matrix[i + x][j + y] + templateCell);
  19.           const letter = rosetta[matrixCell];
  20.           matrix[i + x][j + y] = letter;
  21.         }
  22.       }
  23.     }
  24.     shift();
  25.   }
  26.  
  27.   return matrix.map(x => x.join('')).join('');
  28.   // return matrix;
  29.  
  30.   function doesExist(x) {return x >= 0 && x < matrix.length}
  31.  
  32.   function createMatrix(x) {
  33.     let m = [];
  34.     for (let i = 0; i < x; i++) {m.push(arr.shift().split(' ').map(Number));}
  35.     return m;
  36.   }
  37.  
  38.   function rotate(h) {
  39.     if (h >= 27) {
  40.       let rotations = Math.floor(h / 27);
  41.       return h - rotations * 27;
  42.     }
  43.     return h;
  44.   }
  45.  
  46.   function shift() {
  47.     if (i == matrix.length - 1) {
  48.       i  = 0;
  49.       j += template[0].length;
  50.     } else {
  51.       i += template.length;
  52.       if(i >= matrix.length) {
  53.         i = 0;
  54.         j += template[0].length;
  55.       }
  56.     }
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement