Skip to content

fleissner grille javascript

Can I use a function that allows me to get the hidden message? If so, could you enlighten me, how can I do it?
Using fleissner grille 6 x 6, decipher the hidden message: “lróaon. sg sdersoildsu.:.cc kiomamii”. For this, it must be rotated 90° clockwise. The open grids are at positions [1,1],[4,1],[2,2],[6,2],[5,3],[1,4],[4,4],[ 3,5],[6,5] for the grid in initial position (0 degrees).

  • Note that every time we rotate the grid 90º clockwise, the first row (x-axis) becomes the sixth column (y-axis), the second row becomes the fifth column… and the sixth row becomes the first column.
  • Once you have all the pairs of coordinates of all positions (0º, 90º, 180º and 270º) and ordered, decrypting the message consists of relating the position of each letter of the message with the pair of coordinates:: the first letter of the message decrypted message will be at position [1,1] the second letter of the decrypted message will be at position [4,1] the third letter of the decrypted message will be at position [2,2]. This is what I have so far, any idea how to get the hidden message?
const sideSize    = 6; // Grid 6
const gridPosBase = [[1,1],[4,1],[2,2],[6,2],[5,3],[1,4],[4,4],[3,5],[6,5]];
const encriptedMessage = 'lróaon. sg sdersoildsu.:.cc kiomamii';
const x = 0;
const y = 1;
             
function rotateArray90(gridToTurn) {
  const gridRotated = [];
  gridToTurn.forEach((par, index) => {
    gridRotated[index]    = [];
    gridRotated[index][x] = (sideSize + 1) - par[y];
    gridRotated[index][y] = par[x];
  });
  return gridRotatedOrdered;  // ¡Warning! rotated but no ordered
}

// Rotate the array 90º 3 times and order the coordinates, to obtain all the open cells

function decrypt(text, grid) {
  text = '';
  grid = [];
}
    
const decryptedMessage = decrypt(encriptedMessage, gridPosBase);
console.log(decryptedMessage);

Answer

I had no idea what a Fleissner Grille was but this instructables.com site explained it pretty well.

This is a little tricky in a few places, I hope my explanation helps.

getChar Calculate which character to read out of the “encMsg” based on a grid position. This is the (row – 1) * 6 + (column – 1). Need to subtract 1 from the row and column because arrays and strings start at 0, not 1.

rotateGrid This tricks you a bit. It’s easier if you draw a picture. It becomes clear how to get the new column and row from the old column and row, but THEN you have to sort the array. After the grid is rotated, you have to start reading from the upper-left, so we need to sort the grid.

readGrid This loops through the grid positions and read a character out of the encMsg each time, then returns that chunk of message.

Putting it all together, we read the first chunk, then “rotate & read” three more times to get the final message.

const encMsg = "lróaon. sg sdersoildsu.:.cc kiomamii";
let grid = [
  [1, 1],
  [4, 1],
  [2, 2],
  [6, 2],
  [5, 3],
  [1, 4],
  [4, 4],
  [3, 5],
  [6, 5]
];

const getChar = (x, y, str) => str[x - 1 + (y - 1) * 6];
const rotateGrid = grid =>
  grid
  .map(([x, y]) => [7 - y, x])
  .sort((a, b) => {
    if (a[1] < b[1]) return -1;
    if (a[1] === b[1] && a[0] < b[0]) return -1;
    return 1;
  });
const readGrid = (grid, encMsg) =>
  grid.map(([x, y]) => getChar(x, y, encMsg)).join("");

let unEncMsg = readGrid(grid, encMsg);

for (let ix = 0; ix < 3; ix++) {
  grid = rotateGrid(grid);
  unEncMsg += readGrid(grid, encMsg);
}

console.log(unEncMsg);