DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

Visualizing Promises and Async/Await ๐Ÿคฏ

async await

โ˜๏ธ Check out this all-time classic DEV post