## 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.

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)

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) - ๐ฐ;

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);

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:

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/