Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- const rosetta = {
- 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',
- 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',
- };
- const matrixSize = Number(arr.shift());
- const template = createMatrix(matrixSize);
- let matrix = [...arr].map(x => x.split(' ').map(Number));
- let i = 0;
- let j = 0;
- while (i < matrix.length && j < matrix.length) {
- for (let x = 0; x < template.length; x++) {
- for (let y = 0; y < template[x].length; y++) {
- const templateCell = template[x][y];
- if (doesExist(i + x) && doesExist(j + y)) {
- let matrixCell = rotate(matrix[i + x][j + y] + templateCell);
- const letter = rosetta[matrixCell];
- matrix[i + x][j + y] = letter;
- }
- }
- }
- shift();
- }
- return matrix.map(x => x.join('')).join('');
- // return matrix;
- function doesExist(x) {return x >= 0 && x < matrix.length}
- function createMatrix(x) {
- let m = [];
- for (let i = 0; i < x; i++) {m.push(arr.shift().split(' ').map(Number));}
- return m;
- }
- function rotate(h) {
- if (h >= 27) {
- let rotations = Math.floor(h / 27);
- return h - rotations * 27;
- }
- return h;
- }
- function shift() {
- if (i == matrix.length - 1) {
- i = 0;
- j += template[0].length;
- } else {
- i += template.length;
- if(i >= matrix.length) {
- i = 0;
- j += template[0].length;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement