Skip to content

Instantly share code, notes, and snippets.

@Code-Crash
Created November 1, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Crash/da67f9c89d2c61812623119958b89ec8 to your computer and use it in GitHub Desktop.
Save Code-Crash/da67f9c89d2c61812623119958b89ec8 to your computer and use it in GitHub Desktop.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var r = null;
var rTimes = [];
var winArray = [];
var init = 0;
var counter = 0;
var getValue = function() {
rl.question('', function(input) {
if (init === 0) {
r = parseInt(input);
init++;
getValue();
} else {
rTimes.push(input);
counter++;
if (r == counter) {
rl.close();
main();
} else {
getValue();
}
}
});
};
getValue();
// Helpers
function dec2bin(dec) {
return (dec >>> 0).toString(2);
}
function toggle(value) {
var t = (value === '1') ? '0' : '1';
return t;
}
function setChatAtPosition(str, index, chr) {
if (index > str.length - 1) return str;
return str.substr(0, index) + chr + str.substr(index + 1);
}
function main() {
// Logic
for (var j = 0; j < r; j++) {
var n = parseInt(rTimes[j]);
var winer = null;
var binary = dec2bin(n);
for (var i = 0; i < binary.length; i++) {
var pr = i - 1;
var nt = i + 1;
if (binary[i] === binary[pr]) {
binary = setChatAtPosition(binary, pr, toggle(binary[pr]));
}
if (binary[i] === binary[nt]) {
binary = setChatAtPosition(binary, nt, toggle(binary[nt]));
}
binary = setChatAtPosition(binary, i, toggle(binary[i]));
winer = i + 1;
}
if (n === parseInt(binary, 2) && ((winer % 2) === 0)) {
winArray.push('Y');
} else {
winArray.push('X');
}
}
winArray.forEach(function(winner) {
console.log(winner);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment