This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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