Skip to content

Instantly share code, notes, and snippets.

@brandonheyer
Last active November 18, 2016 20:17
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 brandonheyer/c3b091cd60d3b9f8008af623bd7d9a00 to your computer and use it in GitHub Desktop.
Save brandonheyer/c3b091cd60d3b9f8008af623bd7d9a00 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
var YOUR_PLAYER_ID = 8,
teams = {
0: 'Yates ',
1: 'Wuffalo ',
2: 'Cutler ',
3: 'RBs ',
4: 'FCL ',
5: 'Revv ',
6: 'Dan ',
7: 'Tracy ',
8: 'Cubicles ',
9: 'Wineboys '
}, matchups = [
[
[8, 4],
[0, 1],
[6, 5],
[2, 3],
['9', 7]
],
[
[7, 8],
[5, 0],
[4, 2],
[1, 3],
[9, 6]
],
[
[8, 6],
[7, 2],
[3, 0],
[5, 9],
[1, 4]
]
], permutations = 0, allOutcomes = {};
function team(id, w, l, pf) {
var name = teams[id];
teams[id] = {
name: name,
win: w,
loss: l,
points: pf,
outcomes: []
};
}
function store(results) {
var records = results.trim().split(' '), i, team, win, outcome, outcomes = [];
permutations++;
outcomes.length = 10;
for (i = 0; i < records.length; i++) {
win = records[i][0] === 'W';
team = parseInt(records[i][1], 10);
outcome = outcomes[team];
if (!outcome) {
outcome = outcomes[team] = {
name: teams[team].name,
win: teams[team].win,
loss: teams[team].loss,
points: teams[team].points
};
}
if (win) {
outcome.win++;
} else {
outcome.loss++;
}
}
for (i = 0; i < outcomes.length; i++) {
teams[i].outcomes.push(outcomes[i]);
}
allOutcomes[hashOutcomes(outcomes)] = outcomes;
}
function hashOutcomes(outcomes) {
var i, hash = '';
for (i = 0; i < outcomes.length; i++) {
hash += i + '|' + (outcomes[i].win - outcomes[i].loss) + ' ';
}
return hash;
}
function permute(scheduleIndex, matchupIndex, results) {
var match = matchups[scheduleIndex][matchupIndex];
if (!match) {
if (scheduleIndex === 2) {
store(results);
} else {
permute(scheduleIndex + 1, 0, results);
}
} else {
if (match[0].length) {
permute(scheduleIndex, matchupIndex + 1, results + ' L' + match[0] + ' W' + match[1]);
} else if (match[1].length) {
permute(scheduleIndex, matchupIndex + 1, results + ' W' + match[0] + ' L' + match[1]);
} else {
permute(scheduleIndex, matchupIndex + 1, results + ' W' + match[0] + ' L' + match[1]);
permute(scheduleIndex, matchupIndex + 1, results + ' L' + match[0] + ' W' + match[1]);
}
}
}
function outputOutcome(outcome) {
var i, outcome, team;
outcome.sort(function(a, b) {
return (b.win - a.win) ? b.win - a.win : b.points - a.points;
});
for (i = 0; i < outcome.length; i++) {
console.log(outcome[i].name + '( ' + outcome[i].win + ' Wins, ' + outcome[i].loss + ' Losses, ' + outcome[i].points + ' Points)');
}
}
function output(outcomes) {
var i;
console.log(teams[YOUR_PLAYER_ID].name + ' ' + Math.round(outcomes.length / Object.keys(allOutcomes).length * 100) + '%\t(' + outcomes.length + '/' + Object.keys(allOutcomes).length + ')');
//console.log(outcomes.length);
//console.log(permutations);
//console.log(Math.round(outcomes.length / permutations * 100) + '%');
//console.log('');
/*
for (i = 0; i < outcomes.length; i++) {
outputOutcome(outcomes[i]);
console.log('');
}*/
}
function prune() {
var hashes = Object.keys(allOutcomes), outcomes = Object.values(allOutcomes),
i = 0,
favorable = [], spreads;
for (i = 0; i < hashes.length; i++) {
spreads = hashes[i].trim().split(' ');
spreads.sort(function(a, b) {
var aSplit = a.split('|'), bSplit = b.split('|'), diff;
aSplit[1] = parseInt(aSplit[1], 10);
bSplit[1] = parseInt(bSplit[1], 10);
if (aSplit[1] === bSplit[1]) {
var diff = teams[parseInt(aSplit[0], 10)].points - teams[parseInt(bSplit[0], 10)].points;
if (diff > 0) {
return -1;
}
if (diff < 0) {
return 1;
}
return 0;
}
return bSplit[1] - aSplit[1];
});
var lastSplit, split, spots = 4, lastCnt = 0, last, topFour = spreads.splice(0,4);
/*
lastSplit = topFour[3].split('|');
lastSplit[0] = parseInt(lastSplit[0], 10);
lastSplit[1] = parseInt(lastSplit[1], 10);
split = spreads[0].split('|');
split[0] = parseInt(split[0], 10);
split[1] = parseInt(split[1], 10);
if (false) {
while (split[1] === lastSplit[1] && spreads.length > 1) {
topFour.push(spreads.shift());
split = spreads[0].split('|');
split[0] = parseInt(split[0], 10);
split[1] = parseInt(split[1], 10);
}
}
*/
for (var j = 0; j < topFour.length; j++) {
split = topFour[j].split('|');
split[0] = parseInt(split[0], 10);
split[1] = parseInt(split[1], 10);
if (split[0] === YOUR_PLAYER_ID) {
outcomes[i].hash = hashes[i].trim();
favorable.push(outcomes[i])
break;
}
}
}
output(favorable);
}
team(0, 8, 2, 1090);
team(1, 5, 5, 864.8);
team(2, 5, 5, 882.8);
team(3, 4, 6, 926.5);
team(4, 3, 7, 807.1);
team(5, 7, 3, 1012.6);
team(6, 6, 4, 963.8);
team(7, 5, 5, 919.1);
team(8, 4, 6, 977.1);
team(9, 3, 7, 817.6);
permute(0, 0, '');
for (var i = 0; i < 10; i++) {
YOUR_PLAYER_ID = i;
prune();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment