{
let occurrences = getOccurrences();
function empty() {
for (let i = 0; i < 26; i++) {
for (let j = 0; j < 5; j++) {
if (occurrences[String.fromCharCode("a".charCodeAt(0) + i)][j] != 0) {
return false;
}
}
}
return true;
}
const foundWords = [];
const foundLetters = [{}, {}, {}, {}, {}];
while (!empty()) {
let maxScore = null;
let maxWord = null;
for (const word of sortByOccurrences(words, occurrences)) {
if (foundWords.includes(word)) continue;
let score = 0;
for (let i = 0; i < 5; i++) {
if (foundLetters[i][word.charAt(i)]) {
score--;
}
}
if (maxScore == null || score > maxScore) {
maxScore = score;
maxWord = word;
}
}
foundWords.push(maxWord);
for (let i = 0; i < 5; i++) {
foundLetters[i][maxWord.charAt(i)] = true;
}
occurrences = getOccurrences(foundWords, foundLetters);
}
return foundWords;
}