Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 4, 2016 01:49
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 jianminchen/b5ce95c07e3dd027e3753b8c81f00c74 to your computer and use it in GitHub Desktop.
Save jianminchen/b5ce95c07e3dd027e3753b8c81f00c74 to your computer and use it in GitHub Desktop.
AorB - study JavaScript code - code #1
function getData(input){
var _data = input.split("\n").map(function(line){return line.split(' ');});
var nextLineIndex = 0;
return {
nextLine : function(){
var line = _data[nextLineIndex];
nextLineIndex++;
return line;
}
}
}
var dec = {
'0' : 0,
'1' : 1,
'2' : 2,
'3' : 3,
'4' : 4,
'5' : 5,
'6' : 6,
'7' : 7,
'8' : 8,
'9' : 9,
'A' : 10,
'B' : 11,
'C' : 12,
'D' : 13,
'E' : 14,
'F' : 15
}
var hexBin = {
'0' : '0000',
'1' : '0001',
'2' : '0010',
'3' : '0011',
'4' : '0100',
'5' : '0101',
'6' : '0110',
'7' : '0111',
'8' : '1000',
'9' : '1001',
'A' : '1010',
'B' : '1011',
'C' : '1100',
'D' : '1101',
'E' : '1110',
'F' : '1111'
}
var hex = {
'0000' : '0',
'0001' : '1',
'0010' : '2',
'0011' : '3',
'0100' : '4',
'0101' : '5',
'0110' : '6',
'0111' : '7',
'1000' : '8',
'1001' : '9',
'1010' : 'A',
'1011' : 'B',
'1100' : 'C',
'1101' : 'D',
'1110' : 'E',
'1111' : 'F'
}
function getBinArray(hexArr){
var arr = []
hexArr.forEach(function(hexDigit){
var b = hexBin[hexDigit].split('');
arr.push(b[0], b[1], b[2], b[3]);
});
return arr.map(Number);
}
function binToHex(binArr){
var hexS = '';
for(var i = 0; i < binArr.length - 3; i += 4){
var s = '' + binArr[i] + binArr[i + 1] + binArr[i + 2] + binArr[i + 3];
if(hexS == '' && hex[s] == '0') continue;
hexS += hex[s];
}
if(hexS == '') hexS = '0';
return hexS;
}
function equalize(a, b, c, val){
var m = Math.max(a.length, b.length)
m = Math.max(m, c.length);
while(a.length < m) a.splice(0, 0, val);
while(b.length < m) b.splice(0, 0, val);
while(c.length < m) c.splice(0, 0, val);
}
function processData(input) {
var data = getData(input);
var q = data.nextLine()[0];
while(q-- > 0){
var k = parseInt(data.nextLine()[0]);
var a = data.nextLine()[0].toUpperCase().split('');
var b = data.nextLine()[0].toUpperCase().split('');
var c = data.nextLine()[0].toUpperCase().split('');
equalize(a, b, c, '0');
var ab = getBinArray(a);
var bb = getBinArray(b);
var cb = getBinArray(c);
/*
console.log(ab);
console.log(bb);
console.log(cb);
console.log()
console.log(binToHex(ab));
console.log(binToHex(bb));
console.log(binToHex(cb));
*/
// normalize
for(var i = 0; i < ab.length; i++){
if(cb[i] === (ab[i] | bb[i])) continue;
if(cb[i] == 0){
k -= ab[i];
k -= bb[i];
ab[i] = 0;
bb[i] = 0;
//console.log(i, bb[i])
}
if(cb[i] == 1){
k--;
bb[i] = 1;
}
}
//console.log(ab)
//console.log(bb)
//console.log(cb)
if(k < 0){
console.log(-1); // not possible
continue;
}
// minimize a
for(var i = 0; i < ab.length; i++){
if(k == 0) break;
if(ab[i] == 1 && bb[i] == 1){
k--;
ab[i] = 0;
continue;
}
if(ab[i] == 1 && bb[i] == 0 && k > 1){
k -= 2;
ab[i] = 0;
bb[i] = 1;
}
}
console.log(binToHex(ab));
console.log(binToHex(bb));
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment