Skip to content

Instantly share code, notes, and snippets.

@benzittlau
Created February 12, 2019 22:31
Show Gist options
  • Save benzittlau/89b9fc9f858a0a318fca334b225d6e39 to your computer and use it in GitHub Desktop.
Save benzittlau/89b9fc9f858a0a318fca334b225d6e39 to your computer and use it in GitHub Desktop.
4 By 4 Skyscrapers Tooling
function solvePuzzle (clues) {
// The hint says the value
// that must *at least* be in the adjacent cell.
// 1 -> 4
// 2 -> 3, etc.
// Calculate the possible permutations for each score in advance
// Rotate around in the order of hints, check if there is only
// one permutation that matches the hint and populated values, if
// so then populate it.
let permutationManager = new PuzzlePermutationManager(4);
}
class PuzzlePermutationManager {
constructor(vectorSize) {
this.vectorSize = vectorSize;
this.basisVector = new Array(vectorSize).fill().map((_,i) => { return i + 1 });
this.cachePermutationsForHints();
}
cachePermutationsForHints() {
this._cachedPermutations = PuzzlePermutationManager._generatePermutations(this.basisVector);
this._cachedHintMatches = this.basisVector.map((hint) => {
return PuzzlePermutationManager._filterVectorsForHint(hint, this._cachedPermutations)
});
}
static _generatePermutations(vector) {
if ( vector.length == 1) { return [vector] };
let permutations = [];
for (let i = 0; i < vector.length; i++) {
let firstValue = vector[0];
permutations = permutations.concat(
PuzzlePermutationManager._generatePermutations(vector.slice(1)).map((permutation) => {
return [firstValue].concat(permutation);
})
);
vector.push(vector.shift());
}
return permutations;
}
static _filterVectorsForHint(hint, vectors) {
return vectors.filter((vector) => {
let max = 0, increments = 0;
for(let el of vector) {
if (el > max) {
max = el;
increments++;
}
}
return (increments == hint);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment