Skip to content

Instantly share code, notes, and snippets.

@zacharyliu
Last active August 29, 2015 14:08
Show Gist options
  • Save zacharyliu/754aee70c5ee9d4af073 to your computer and use it in GitHub Desktop.
Save zacharyliu/754aee70c5ee9d4af073 to your computer and use it in GitHub Desktop.
HackPrinceton Fall 2014 second round acceptances script
var rawData = require('./all_users_data_2014_10_24.json');
// console.log(rawData);
var skipTeams = process.argv;
skipTeams.splice(2);
var teams = {};
var sizes = {};
var mappings = {
"MIT": "MIT",
"Massachusetts Institute of Technology": "MIT",
"Boston University": "BU",
"McGill": "McGill",
"Toronto": "Toronto",
"Waterloo": "Waterloo",
"Buffalo": "NY",
"Rochester Institute Of Technology": "NY",
"Cornell": "NY",
"Binghamton": "NY",
"Princeton": "Princeton"
};
function getMapping(school) {
for (name in mappings) {
if (school.indexOf(name) != -1) {
return mappings[name];
}
}
return "lottery";
}
var quotas = {};
var results = {};
var quotaNames = [];
function addQuota(name, num) {
quotas[name] = num;
results[name] = [];
quotaNames.push(name);
}
addQuota("lottery", 41);
addQuota("MIT", 9);
addQuota("BU", 4);
addQuota("McGill", 15);
addQuota("NY", 4);
addQuota("Toronto", 5);
addQuota("Waterloo", 4);
addQuota("Princeton", -1);
rawData.forEach(function(user) {
if (skipTeams.indexOf(user.team) == -1 && user.status == "Pending") {
user.mapping = getMapping(user.school);
if (!(user.team in teams)) {
teams[user.team] = [];
}
teams[user.team].push(user);
}
});
for (team in teams) {
var size = teams[team].length;
if (!(size in sizes)) {
sizes[size] = [];
}
sizes[size].push(team);
}
for (size in sizes) {
if (size > 5) {
delete sizes[size];
}
}
function getRandomTeamNumber() {
var total = 0;
for (size in sizes) {
total += sizes[size].length * size;
}
var rand = Math.floor(Math.random() * total);
var total2 = 0;
for (size in sizes) {
if ((rand - total2) < sizes[size].length * size) {
return sizes[size][Math.floor((rand - total2) / size)];
}
total2 += sizes[size].length * size;
}
}
function checkAddition(teamNumber) {
var postSizes = {};
quotaNames.forEach(function(name) {
postSizes[name] = results[name].length;
});
var hasNoQuota = true;
teams[teamNumber].forEach(function(user) {
if (quotas[user.mapping] != -1) {
hasNoQuota = false;
}
postSizes[user.mapping]++;
});
if (hasNoQuota) {
return false;
}
for (name in postSizes) {
if (postSizes[name] > quotas[name] && quotas[name] != -1) {
return false;
}
}
return true;
}
function accept(teamNumber) {
// console.log("Accepting team #", teamNumber, "with", teams[teamNumber].length, "members");
teams[teamNumber].forEach(function(user) {
results[user.mapping].push(user);
});
sizes[teams[teamNumber].length].splice(sizes[teams[teamNumber].length].indexOf(teamNumber), 1);
}
var remainingCount;
do {
var teamNumber = getRandomTeamNumber();
if (checkAddition(teamNumber)) {
accept(teamNumber);
remainingCount = 0;
for (name in quotas) {
if (quotas.hasOwnProperty(name) && quotas[name] != -1) {
remainingCount += quotas[name] - results[name].length;
}
}
}
// console.log(" > " + remainingCount + " slots to fill");
// console.log(teamNumber);
} while (remainingCount > 0);
for (quotaName in results) {
results[quotaName].forEach(function(user) {
console.log(user.email + "\t" + user.team + "\t" + user.school + "\t" + quotaName);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment