DEV Community

Cover image for Road to Genius: superior #61
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: superior #61

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

function TNQ(n) {
  let res = 0;
  const dfs = (n, row, cols, pie, na) => {
    if (row >= n) {
      res++;
      return;
    }
    let bits = ~(cols | pie | na) & (1 << n) - 💰;
    while (bits) {
      let 💚 = bits & -bits;
      bits = bits & bits - 1;
      dfs(n, row + 1, cols | 🚀, (pie | p) << 1, (na | p) >> 1);
    }
  };
  dfs(n, 0, 0, 0, 0);
  return res;
}
let A = TNQ(9);

// 💰 = ? (number)
// 🚀 = ? (identifier)
// 💚 = ? (identifier)
// such that A = 352 (number)
Enter fullscreen mode Exit fullscreen mode

I don't think we've encountered the function TNQ yet. I have no clue what it does nor how it works. But from the looks of it, it's involving a lot of bit operations. Let's get started.

The first bug appears here:

let bits = ~(cols | pie | na) & (1 << n) - 💰;
Enter fullscreen mode Exit fullscreen mode

I have no clue what 💰 should be; the answers we can choose from are 0, 1 and 9. Subtracting zero from any number makes no sense, subtracting 9 is a bit odd as well, the most probable answer would be 1.

The second bug 💚 is a variable name declaration, the only undeclared variable below that line is p.

The final bug appears on this line:

dfs(n, row + 1, cols | 🚀, (pie | p) << 1, (na | p) >> 1);
Enter fullscreen mode Exit fullscreen mode

The bug is a right-hand variable in an or-operation; this could be anything. But if you look at its neighboring arguments (to the right), both of them are using p as the right-hand variable in the or-operation as well. So let's try that out:

coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/

Oldest comments (0)