// Run with Node.js. Both checks are finite and use exact integer arithmetic. function assert(condition, message) { if (!condition) throw new Error(message); } function* tuples(n, k, used = new Set(), row = []) { if (row.length === k) { yield row.slice(); return; } for (let i = 0; i < n; i++) { if (used.has(i)) continue; used.add(i); row.push(i); yield* tuples(n, k, used, row); row.pop(); used.delete(i); } } // Check 1. Explicit eight-vertex distribution. Test representatives for // each possible set of red pairs within an ordered triple of inputs. function verifyPermutationLaw() { const inputs = [[0, 2, 4], [0, 1, 2], [0, 2, 1], [2, 0, 1]]; const counts = inputs.map(() => new Uint32Array(8 ** 3)); let total = 0; for (const p of tuples(8, 8)) { const blue = new Array(8); for (let i = 0; i < 8; i++) blue[p[i]] = p[i ^ 1]; const seen = new Set(); const sizes = []; for (let i = 0; i < 8; i++) { if (seen.has(i)) continue; const stack = [i]; let size = 0; while (stack.length) { const v = stack.pop(); if (seen.has(v)) continue; seen.add(v); size++; stack.push(v ^ 1, blue[v]); } sizes.push(size); } let weight = 1; if (sizes.includes(8)) weight = 0; if (sizes.length === 2 && sizes.every(size => size === 4)) weight = 5; total += weight; inputs.forEach(([i, j, k], row) => { counts[row][64 * p[i] + 8 * p[j] + p[k]] += weight; }); } assert(total === 40320, "Incorrect total weight"); for (const [i, j, k] of tuples(8, 3)) { for (const row of counts) { assert(row[64 * i + 8 * j + k] === 120, "Nonuniform triple image"); } } } // Check 2. Work with alternating polygons, independently of permutation // probabilities. Compare the complete histogram of induced red/blue patterns. function patternHistogram(lengths, k) { const red = [], blue = []; let offset = 0; for (const length of lengths) { for (let i = 0; i < length; i += 2) { red[offset + i] = offset + i + 1; red[offset + i + 1] = offset + i; const a = offset + i + 1, b = offset + (i + 2) % length; blue[a] = b; blue[b] = a; } offset += length; } const histogram = new Map(); for (const row of tuples(offset, k)) { let code = 0; for (let j = 0; j < k; j++) { for (let i = 0; i < j; i++) { code = 4 * code + Number(red[row[i]] === row[j]) + 2 * Number(blue[row[i]] === row[j]); } } histogram.set(code, (histogram.get(code) || 0) + 1); } return histogram; } verifyPermutationLaw(); for (const [k, before, after] of [[3, [8], [4, 4]], [4, [2, 12], [2, 6, 6]], [5, [14], [6, 8]]]) { const a = patternHistogram(before, k), b = patternHistogram(after, k); assert(a.size === b.size, "Different pattern support"); for (const [pattern, count] of a) assert(b.get(pattern) === count, "Different pattern count"); } console.log("Passed the eight-vertex permutation law and three polygon splits.");