DEV Community

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

Posted on

3

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/

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay