Randomized the order of the edges as well as the vertices

Previously, edge order would stay constant while vertex order was
shuffled. This way, there is even less opportunity for a player to
reverse-engineer the initial bipartite graph.
This commit is contained in:
Undeemiss
2022-04-20 21:23:55 -05:00
parent 951221578a
commit 8d026e4f10

View File

@@ -1336,22 +1336,28 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
edges.push([a, b]);
}
//Randomize array in-place using Durstenfeld shuffle algorithm
const shuffler = Array.from(Array(n + m).keys());
for (let i = shuffler.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffler[i], shuffler[j]] = [shuffler[j], shuffler[i]];
//Randomize array in-place using Durstenfeld shuffle algorithm.
function shuffle(array: any[]): void {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
//Replace instances of the original vertex names in-place
const vertexShuffler = Array.from(Array(n + m).keys());
shuffle(vertexShuffler);
for (let i = 0; i < edges.length; i++) {
edges[i] = [shuffler[edges[i][0]], shuffler[edges[i][1]]];
edges[i] = [vertexShuffler[edges[i][0]], vertexShuffler[edges[i][1]]];
if (edges[i][0] > edges[i][1]) {
//Enforce lower numbers come first
[edges[i][0], edges[i][1]] = [edges[i][1], edges[i][0]];
}
}
//Shuffle the order of the edges themselves, as well
shuffle(edges);
return [n + m, edges];
},
solver: (data: [number, [number, number][]], ans: string): boolean => {