Skip to content

Instantly share code, notes, and snippets.

@mappum
Created January 21, 2012 01:34
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 mappum/1650672 to your computer and use it in GitHub Desktop.
Save mappum/1650672 to your computer and use it in GitHub Desktop.
/*
* Execute with node.js, and pass the input via stdin, like so:
* node main.js < input.txt
*/
var MESSAGE = 'HACKERCUP';
/**
* The entry point, reads a number n then n datasets from stdin
* and passes them into run()
*/
function main() {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
var input = chunk.split('\n');
for(var i = 1; i <= input[0]; i++) {
var line = input[i].split(' ');
run(line, i);
}
});
}
/**
* Called when a line is read in by main
*/
function run(data, iteration) {
var string = '',
running = true,
messages = 0;
for(var i = 0; i < data.length; i++) string += data[i] + ' ';
while(running) {
for(var j = 0; j < MESSAGE.length; j++) {
var charI = string.indexOf(MESSAGE.charAt(j));
if(data.length > 0 && charI > -1) {
string = string.substring(0, charI) + string.substring(charI + 1);
} else {
running = false;
break;
}
}
if(running) messages++;
}
console.log('Case #' + iteration + ': ' + messages);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment