Skip to content

Instantly share code, notes, and snippets.

@conorhastings
Last active August 31, 2018 14:31
Show Gist options
  • Save conorhastings/bfd2b8eee453d534b95513eb202480ff to your computer and use it in GitHub Desktop.
Save conorhastings/bfd2b8eee453d534b95513eb202480ff to your computer and use it in GitHub Desktop.
scheduling
{
"1": [
[1, 2],
[1, 3],
[1, 9],
[1, 6],
[1, 3],
[1, 9],
[1, 6],
[1, 5],
[1, 12],
[1, 10],
[1, 4],
[1, 7],
[1, 8]
],
"2": [
[2, 1],
[2, 4],
[2, 8],
[2, 12],
[2, 4],
[2, 8],
[2, 12],
[2, 3],
[2, 9],
[2, 10],
[2, 6],
[2, 5],
[2, 11]
],
"3": [
[3, 4],
[3, 1],
[3, 9],
[3, 6],
[3, 1],
[3, 9],
[3, 6],
[3, 2],
[3, 7],
[3, 8],
[3, 11],
[3, 10],
[3, 12]
],
"4": [
[4, 3],
[4, 2],
[4, 8],
[4, 12],
[4, 2],
[4, 8],
[4, 12],
[4, 1],
[4, 5],
[4, 10],
[4, 7],
[4, 9],
[4, 11]
],
"5": [
[5, 6],
[5, 7],
[5, 10],
[5, 11],
[5, 7],
[5, 10],
[5, 11],
[5, 1],
[5, 2],
[5, 4],
[5, 8],
[5, 9],
[5, 12]
],
"6": [
[6, 5],
[6, 1],
[6, 3],
[6, 9],
[6, 1],
[6, 3],
[6, 9],
[6, 2],
[6, 11],
[6, 7],
[6, 12],
[6, 10],
[6, 8]
],
"7": [
[7, 8],
[7, 5],
[7, 10],
[7, 11],
[7, 5],
[7, 10],
[7, 11],
[7, 1],
[7, 3],
[7, 4],
[7, 6],
[7, 9],
[7, 12]
],
"8": [
[8, 7],
[8, 2],
[8, 4],
[8, 12],
[8, 2],
[8, 4],
[8, 12],
[8, 1],
[8, 3],
[8, 5],
[8, 6],
[8, 11],
[8, 10]
],
"9": [
[9, 10],
[9, 1],
[9, 3],
[9, 6],
[9, 1],
[9, 3],
[9, 6],
[9, 2],
[9, 4],
[9, 5],
[9, 7],
[9, 11],
[9, 12]
],
"10": [
[10, 9],
[10, 5],
[10, 7],
[10, 11],
[10, 5],
[10, 7],
[10, 11],
[10, 1],
[10, 2],
[10, 3],
[10, 4],
[10, 6],
[10, 8]
],
"11": [
[11, 12],
[11, 5],
[11, 7],
[11, 10],
[11, 5],
[11, 7],
[11, 10],
[11, 2],
[11, 3],
[11, 4],
[11, 6],
[11, 8],
[11, 9]
],
"12": [
[12, 11],
[12, 2],
[12, 4],
[12, 8],
[12, 2],
[12, 4],
[12, 8],
[12, 1],
[12, 3],
[12, 5],
[12, 6],
[12, 7],
[12, 9]
]
}
const rivals = [
[1, 2],
[2, 1],
[3, 4],
[4, 3],
[5, 6],
[6, 5],
[7, 8],
[8, 7],
[9, 10],
[10, 9],
[11, 12],
[12, 11]
];
const divisions = [[1, 3, 9, 6], [2, 4, 8, 12], [5, 7, 10, 11]];
const schedules = {};
const teamsPlayed = {};
for (let i = 0; i < 12; i++) {
schedules[i + 1] = [rivals[i]];
teamsPlayed[i + 1] = [rivals[i].find(rival => rival !== i + 1)];
}
for (let i = 0; i < divisions.length; i++) {
const division = divisions[i];
for (let j = 0; j < division.length; j++) {
const currTeam = division[j];
const divisionWithoutTeam = division.filter(team => team !== currTeam);
schedules[currTeam].push(
...divisionWithoutTeam.map(team => [currTeam, team])
);
schedules[currTeam].push(
...divisionWithoutTeam.map(team => [currTeam, team])
);
teamsPlayed[currTeam].push(...divisionWithoutTeam);
}
}
for (let i = 0; i < 12; i++) {
const currTeam = i + 1;
const divisionIndex = divisions.findIndex(div => div.includes(currTeam));
let nonDivisionTeams = divisions
.filter((_, index) => divisionIndex !== index)
.reduce((flattened, division) => flattened.concat(division), [])
.filter(team => {
return !teamsPlayed[currTeam].includes(team) && schedules[team].length !== 13
});
const gamesPlayed = schedules[currTeam].length;
for (let j = 0; j < 13 - gamesPlayed; j++) {
const randomNonDivisionTeam = nonDivisionTeams[Math.floor(Math.random() * nonDivisionTeams.length)];
schedules[currTeam].push([currTeam, randomNonDivisionTeam]);
schedules[randomNonDivisionTeam].push([randomNonDivisionTeam, currTeam]);
teamsPlayed[currTeam].push(randomNonDivisionTeam);
teamsPlayed[randomNonDivisionTeam].push(currTeam);
nonDivisionTeams = nonDivisionTeams.filter(team => team !== randomNonDivisionTeam);
}
}
console.log(schedules);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment