Created
July 29, 2016 14:50
-
-
Save acceptable-security/270784808311d4fe5d0a283855bb2839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPrime(n) { | |
for ( let i = 2; i < n / 2; i++ ) { | |
if ( n % i == 0 ) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function createMatrix(size) { | |
const array = []; // Array to store the matrix | |
const tmp = []; // First row, containing numbers 1 .. size | |
for ( let i = 1; i <= size; i++ ) { | |
tmp.push(i); | |
} | |
array.push(tmp); | |
// Initialize the rest of the rows as 0s | |
for ( let i = 0; i < size - 1; i++ ) { | |
const tmp = []; | |
for ( let j = 0; j < size; j++ ) { | |
tmp.push(0); | |
} | |
array.push(tmp); | |
} | |
// Fill in the array given the rules | |
for ( let y = 1; y < size; y++ ) { | |
array[y][0] = y + 1; | |
for ( let x = 1; x < size; x++ ) { | |
const find = array[y - 1].indexOf(array[y][x - 1]) + 1; | |
if ( find == size ) { | |
array[y][x] = 1; | |
} | |
else { | |
array[y][x] = array[y - 1][find] + 1; | |
} | |
} | |
} | |
return array; | |
} | |
function verifyMatrix(matrix, debug=True) { | |
// 3 rules: | |
// 1. no chain has one person draw twice | |
// 2. no person draws two chains on the same day | |
// 3. no person receives a drawing from the same person more than once | |
// Verify rule #1 | |
for ( let y = 0; y < matrix.length; y++ ) { | |
let good = true; | |
const check = {}; | |
for ( let x = 0; x < matrix[y].length; x++ ) { | |
if ( check[matrix[y][x]] ) { | |
good = false; | |
break; | |
} | |
check[matrix[y][x]] = true; | |
} | |
if ( !good ) { | |
if ( debug ) console.log("Rule #1 broken on line " + (y + 1).toString()); | |
return false; | |
} | |
} | |
// Verify rule #2 | |
for ( let x = 0; x < matrix.length; x++ ) { | |
let good = true; | |
const check = {}; | |
for ( let y = 0; y < matrix[x].length; y++ ) { | |
if ( check[matrix[y][x]] ) { | |
good = false; | |
break; | |
} | |
check[matrix[y][x]] = true; | |
} | |
if ( !good ) { | |
if ( debug ) console.log("Rule #2 broken on vertical line " + (x + 1).toString()); | |
return false; | |
} | |
} | |
//Verify rule #3 | |
const verf = {}; | |
for ( let i = 0; i < matrix.length; i++ ) { | |
for ( let j = 0; j < matrix[i].length; j++ ) { | |
if ( i != j ) { | |
verf[i.toString() + "_" + j.toString()] = false; | |
} | |
} | |
} | |
for ( let y = 0; y < matrix.length; y++ ) { | |
for ( let x = 0; x < matrix[y].length - 1; x++ ) { | |
const first = matrix[y][x]; | |
const second = matrix[y][x + 1]; | |
const check = first.toString() + "_" + second.toString(); | |
if ( verf[check] ) { | |
if ( debug ) console.log("Rule #3 at location (" + x.toString() + ", " + y.toString() + ")"); | |
return false; | |
} | |
verf[check] = true; | |
} | |
} | |
if ( debug ) console.log("All rules passing."); | |
return true; | |
} | |
function debugMatrix(matrix) { | |
for ( let y = 0; y < matrix.length; y++ ) { | |
let row = ""; | |
for ( let x = 0; x < matrix[y].length; x++ ) { | |
row = row + matrix[y][x].toString() + "\t"; | |
} | |
console.log(row); | |
} | |
} | |
for ( let i = 1; i < 100; i++ ) { | |
const matrix = createMatrix(i); | |
const worked = verifyMatrix(matrix, false); | |
if ( worked ) { | |
if ( isPrime(i + 1) ) { | |
console.log("Brackets is the best"); | |
} | |
else { | |
console.log("BRB throwing self off cliff"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment