Skip to content

Instantly share code, notes, and snippets.

@clux
Created June 16, 2014 14:46
Show Gist options
  • Save clux/e6f748c8bd9539d8ae1a to your computer and use it in GitHub Desktop.
Save clux/e6f748c8bd9539d8ae1a to your computer and use it in GitHub Desktop.
groupstage module vs fifa2014
var seeds = [
// top 8 seeds
'BRZ', // brazil
'SPA', // spain
'GER', // germany
'ARG', // argentina
'COL', // columbia
'BEL', // belgium
'URU', // uruguay
'SWI', // switzerland
// bottom seeds
'NED', // netherlands
'ITA', // italy
'ENG', // england
'CHI', // chile
'USA', // united states
'POR', // portugal
'GRE', // greece
'BIH', // bosnia and herzegovina
'CIV', // ivory coast
'CRO', // croatia
'RUS', // russia
'FRA', // france
'ECU', // ecuador
'GHA', // ghana
'MEX', // mexico
'CRC', // costa rica
'ALG', // algeria
'NGA', // nigeria
'HON', // honduras
'JPN', // japan
'IRN', // iran
'KOR', // south korea
'AUS', // australia
'CMR' // cameroon
];
var assert = require('assert');
assert(seeds.length === 8*4, 'have not missed out any teams');
// expected sum of seeds is sum of integers up to seeds.length divided by num groups
var seedSum = (seeds.length*(seeds.length+1)/2) / 8;
console.log('fifa groups 2014:');
var fifaGroups = [
['BRZ', 'MEX', 'CMR', 'CRO'],
['NED', 'CHI', 'AUS', 'SPA'],
['COL', 'CIV', 'JPN', 'GRE'],
['CRC', 'ITA', 'ENG', 'URU'],
['FRA', 'SWI', 'ECU', 'HON'],
['ARG', 'IRN', 'NGA', 'BIH'],
['GER', 'POR', 'GHA', 'USA'],
['BEL', 'ALG', 'RUS', 'KOR']
];
var calculateSum = function (group) {
return group.reduce(function (sum, code) {
var idx = seeds.indexOf(code);
assert(idx >= 0, 'index exists for ' + code);
return sum + (idx + 1);
}, 0);
};
var masterSum = 0;
fifaGroups.forEach(function (group, i) {
var sum = calculateSum(group);
console.log(' Group', (i + 1) + ':', group.join(', '), '( seed sum is ' + sum + ' )');
masterSum += sum;
});
assert(masterSum === seedSum*8, 'we did math correctly');
// so sort by strength:
console.log('Toughest groups:')
fifaGroups.sort(function (g1, g2) {
return calculateSum(g1) - calculateSum(g2);
}).slice(0, 3).forEach(function (group, i) {
var sum = calculateSum(group);
console.log(' #' + (i+1) + ':', group.join(', '), '(' + sum + ')');
});
console.log('\nactually fair groups:');
var GS = require('./');
var gs = new GS(8*4, { groupSize: 4 });
[1,2,3,4,5,6,7,8].forEach(function (g) {
var sum = 0;
var ps = [];
gs.players({ s: g }).forEach(function (p) {
ps.push(seeds[p-1]);
sum += p;
});
console.log(' Group', g + ':', ps.join(', '), '( seed sum is ' + sum + ' )');
});
@clux
Copy link
Author

clux commented Jun 16, 2014

Final note. GroupStage module that generated the fair groups did not just pick ANY combination to ensure the sum was 66. It took one from top 8 (highest tier), one from 9-16 (second tier), one from 17-24 (third tier), and one from 25-32 (bottom tier).

This is always possible to do when the size of each group is the same even number (in this case 4).

@clux
Copy link
Author

clux commented Jun 18, 2014

How bad could it have been? Looking at the draw procedures, it is possible to get the following combination of groups:

var toughestPossible = ['BRZ', 'NED', 'USA', 'ITA']; // seed sum 33
var weakestPossible = ['SWI', 'CMR', 'AUS', 'NGA']; // seed sum 97
// 6 other groups

The toughest one relies on Italy being picked as the extraneous european team and it being matched up with Brazil as the pot X winner. Entirely possible, but obviously much worse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment