JSFiddle - React, Tailwind, and code Playground
by wio_dude
HTML
<div id="output"></div>
JavaScript
Array.prototype.equals = function(that) {
if (this.length != that.length) {
return false;
}
for (var i = 0; i < this.length; i++) {
if (this[i] != that[i]) {
return false;
}
}
return true;
}
Array.prototype.unique_push = function(x) {
for (var i = 0; i < this.length; i++) {
if (this[i].equals(x)) {
return;
}
}
this.push(x);
}
Array.prototype.push_many = function(x, n) {
for (var i = 0; i < n; i++) {
this.push(x);
}
return this;
}
var UI = {
'output': document.getElementById('output'),
'print' : function(x) {
UI.output.innerHTML += x;
},
'println' : function(x) {
UI.print(x + '<br />');
}
}
function factorial(n) {
return permutations(n, n);
}
function permutations(n, k) {
var result = 1;
for (var i = 0; i < k; i++) {
result *= n - i;
}
return result;
}
function power(n, k) {
var result = 1;
for (var i = 0; i < k; i++) {
result *= n;
}
return result;
}
function simulate(team_count) {
var matches = factorial(team_count - 1);
var outcomes = 1 << matches;
var scorehash = {};
UI.println('team_count = ' + team_count);
UI.println('matches = ' + matches);
UI.println('outcomes = ' + outcomes);
for (var i = 0; i < outcomes; i++) {
var score = [].push_many(0, team_count);
var matchbit = 0;
for (var n = 0; n < team_count; n++) {
for (var k = n + 1; k < team_count; k++) {
if (i & (1 << matchbit)) {
score[n]++;
} else {
score[k]++;
}
matchbit++
}
}
var scorekey = 0;
for (var j = 0; j < team_count; j++) {
scorekey += score[j] * power(team_count, j);
}
UI.println('i = ' + i);
UI.println(score);
...