Created
July 3, 2019 00:47
-
-
Save dtudury/cd93e23069de3ecbc2e28bc972fbfddc to your computer and use it in GitHub Desktop.
solution to eight queens puzzle
This file contains hidden or 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 place (placed, unplaced) { | |
if (!unplaced.length) return [placed] | |
let validPlacements = [] | |
unplaced.forEach(unplacedRow => { | |
if (placed.every((placedRow, i) => { | |
const d = placed.length - i | |
return placedRow + d !== unplacedRow && placedRow - d !== unplacedRow | |
})) { | |
let possibleUnplaced = unplaced.filter(row => row !== unplacedRow) | |
validPlacements = [...validPlacements, ...place([...placed, unplacedRow], possibleUnplaced)] | |
} | |
}) | |
return validPlacements | |
} | |
const rows = [] | |
for (let i = 0; i < 8; i++) rows.push(i) | |
console.log(place([], rows)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment