Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code Challenges
var input = "BMC_TEST_INPUT_MAGIC"
input = input.split(',');
var evenRow = "";
var oddRow = "";
var board = "";
// gen columns
for(i = 0; input[0] > i; i++){
if(i % 2 == 1){
evenRow += 1;
oddRow += 0;
}else{
evenRow += 0;
oddRow += 1;
}
}
// gen rows
for(i = 0; input[1] > i; i++){
if(i % 2 != 1){
board += evenRow + "\n";
}else{
board += oddRow + "\n";
}
}
process.stdout.write(board);
var isPrime = function(num) {
for(var i = 2; i*i <= num; i++) {
if(num % i == 0) { return false; }
}
return true;
}
var rotationsArePrime = function(num) {
var arr = num.toString().split("");
var rotations = [];
// Pop off last element and stick it on front.
arr.unshift(arr.pop());
var currNum = arr.join("");
while(currNum != num) {
if(!isPrime(currNum)) { return false; }
arr = currNum.split("");
arr.unshift(arr.pop());
currNum = arr.join("");
}
return true;
}
var input = "BMC_TEST_INPUT_MAGIC";
var primes = 0;
for(var i = 2; i <= input; i++) {
if(isPrime(i)) {
if(rotationsArePrime(i)) {
//console.log(i);
primes++;
}
}
}
console.log(primes);
function getCardIndex(card){
var card = card.split("");
switch(card[0]){
case "T":
card[0] = 10;
break;
case "J":
card[0] = 11;
break;
case "Q":
card[0] = 12;
break;
case "K":
card[0] = 13;
break;
case "A":
card[0] = 14;
break;
default:
card[0] = parseInt(card[0]);
break;
}
switch(card[1]){
case "S":
card[1] = 0;
break;
case "C":
card[1] = 1;
break;
case "H":
card[1] = 2;
break;
case "D":
card[1] = 3;
break;
}
return (card[0]-2)+13*card[1];
}
var input = "BMC_TEST_INPUT_MAGIC";
var cards = input.split(",");
//console.log(cards);
//console.log(getCardIndex(cards[0]));
var hashtable = Array(52);
for(var i = 0; i < 52; i++)
hashtable[i] = 0;
//console.log(hashtable);
for(var i = 0; i < cards.length; i++){
hashtable[getCardIndex(cards[i])]+=1;
}
//console.log(hashtable);
var min = Number.MAX_VALUE;
for(var i = 0; i < 52; i++){
if(hashtable[i] < min)
min = hashtable[i];
}
console.log(min);
var str = "BMC_TEST_INPUT_MAGIC";
var pieces = str.split(':');
var numbers = pieces[1].split(',');
for(i = 1; i <= pieces[0]; i++) {
if(numbers.indexOf(i.toString()).toString() == -1) {
process.stdout.write(i.toString());
}
}
function digitSum(n)
{
var sumUpTo = [0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45];
var ZeroToTenSum = 45;
var total = 0;
var leftSide = n;
var rightSide = 0;
var placeVal = 1;
while(leftSide > 0)
{
total += Math.floor(leftSide/10) * ZeroToTenSum * placeVal ;
total += sumUpTo[leftSide%10]*placeVal + (leftSide%10)*(rightSide+1);
rightSide += (leftSide%10)*placeVal;
leftSide = Math.floor(leftSide / 10);
placeVal *= 10;
}
return total;
}
var testnum = parseInt("BMC_TEST_INPUT_MAGIC");
var outputVal = digitSum(testnum);
console.log(outputVal);
var input = "BMC_TEST_INPUT_MAGIC";
var number = parseInt(input);
var squares = Math.pow((number << 1) + 1, 2);
if (number === 1) {
console.log(--squares);
return;
}
console.log(squares);
//sampleInput = "X X X O\nX O X O\nX X X O";
sampleInput = "BMC_TEST_INPUT_MAGIC";
arrayBoard = [];
width = 0;
height = 0;
readInput = function(input) {
var rows = input.split("\n");
height = rows.length;
width = rows[0].split(" ").length;
for(var i = 0; i < height; i++) {
var thisRow = rows[i].split(" ");
for(var j = 0; j < width; j++) {
arrayBoard.push(thisRow[j]);
}
}
}
getMineCount = function(cell) {
var functions = [getNorth, getNE, getEast, getSE, getSouth, getSW, getWest, getNW];
var mineCount = 0;
for(var i = 0; i < functions.length; i++) {
var curFunc = functions[i];
mineCount += curFunc(cell);
}
return mineCount;
}
UP = 0;
LEFT = 1;
DOWN = 2;
RIGHT = 3;
isWall = function(cell, direction) {
var numCells = width*height;
switch(direction) {
case UP:
if(cell < width) { return true; } // top cells
return false;
case LEFT:
if(cell % width == 0) { return true; } // left cells
return false;
case DOWN:
if(cell >= width * (height - 1)) { return true; } // bottom row
return false;
case RIGHT:
if(cell % width == (width - 1) ) { return true; } // right cells
return false;
}
}
getNorth = function(cell) {
if(isWall(cell, UP)) { return 0; }
if(arrayBoard[cell-width] == 'X') { return 1; }
return 0;
}
getNE = function(cell) {
if(isWall(cell, UP)) { return 0; }
if(isWall(cell, RIGHT)) { return 0;}
if(arrayBoard[cell-width+1] == 'X') { return 1; }
return 0;
}
getEast = function(cell) {
if(isWall(cell, RIGHT)) { return 0;}
if(arrayBoard[cell+1] == 'X') { return 1; }
return 0;
}
getSE = function(cell) {
if(isWall(cell, DOWN)) { return 0; }
if(isWall(cell, RIGHT)) { return 0;}
if(arrayBoard[cell+width+1] == 'X') { return 1; }
return 0;
}
getSouth = function(cell) {
if(isWall(cell, DOWN)) { return 0; }
if(arrayBoard[cell+width] == 'X') { return 1; }
return 0;
}
getSW = function(cell) {
if(isWall(cell, DOWN)) { return 0; }
if(isWall(cell, LEFT)) { return 0;}
if(arrayBoard[cell+width-1] == 'X') { return 1; }
return 0;
}
getWest = function(cell) {
if(isWall(cell, LEFT)) { return 0; }
if(arrayBoard[cell-1] == 'X') { return 1; }
return 0;
}
getNW = function(cell) {
if(isWall(cell, UP)) { return 0; }
if(isWall(cell, LEFT)) { return 0;}
if(arrayBoard[cell-width-1] == 'X') { return 1; }
return 0;
}
printOutput = function() {
var output = "";
for(var i = 0; i < height; i++) {
var toPrint = "";
var thisLine = [];
for(var j = 0; j < width; j++) {
var cellNum = (i*width)+j;
if(arrayBoard[cellNum] == 'X') {
thisLine.push("X");
//toPrint = toPrint+"X ";
} else {
thisLine.push(getMineCount(cellNum));
//toPrint = toPrint+getMineCount(cellNum)+" ";
}
}
output = output + thisLine.join(" ")+"\n";
}
console.log(output);
}
readInput(sampleInput);
printOutput();
var inputData = "BMC_TEST_INPUT_MAGIC";
if (inputData == "") {
console.log(0);
return;
}
var sortedLetters = inputData.split("").sort().join("").replace(/(.)(?=\1)/g, "").split("");
var feed = [];
for (var i = 0; i < inputData.length; i++) {
var currentLetter = inputData[i];
for (var j = 0; j < sortedLetters.length; j++) {
if(currentLetter == sortedLetters[j]) {
feed.push(j + 1);
}
}
}
var factorials = [0];
var factorial = 1;
for (var i = 0; i < inputData.length - 1; i++) {
factorial *= i + 1;
factorials.push(factorial);
}
var returnValue = 0;
while(feed.length) {
var current = feed.shift();
var currentPos = 0;
for (i = 0; i < feed.length; i++) {
currentPos += feed[i] < current ? 1 : 0;
}
returnValue += factorials[feed.length] * currentPos;
//returnValue += factorials
}
console.log(returnValue + 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.