Skip to content

Instantly share code, notes, and snippets.

@nobita4176
Created February 1, 2016 11:45
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 nobita4176/69125131146969021028 to your computer and use it in GitHub Desktop.
Save nobita4176/69125131146969021028 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
Number.prototype.times = function(f) {
for (var i = 0; i < this; i++) {
f(this);
}
};
Array.prototype.shuffle = function() {
var a = [].concat(this);
for (var i = this.length - 1; i > 0; i = 0 | i - 1) {
var j = 0 | Math.random() * (i + 1);
var t = a[i];
a[i] = a[j];
a[j] = t;
}
return a;
};
var read_stdin = function(f) {
var input = '';
process.stdin.on('data', function(chunk) {
input += chunk;
});
process.stdin.on('end', function() {
f(input);
});
};
var create_deck_list = function(input) {
var l = [];
input.split('\n').forEach(function(e) {
if (/^\d+\s+/.test(e) === false) {return;}
var count = e.split(/\s+/)[0] | 0;
var name = e.split(/\s+/)[1];
count.times(function() {l.push(name);});
});
return l;
};
var shuffle_then_draw = function(i) {
create_deck_list(i)
.shuffle()
.slice(0, 7)
.forEach(function(e) {
console.log(e);
});
};
if (process.argv.length >= 3) {
var fs = require('fs');
fs.readFile(process.argv[2], 'utf8', function(e, text) {
shuffle_then_draw(text);
});
} else {
read_stdin(shuffle_then_draw);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment