-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindWiddle.js
More file actions
65 lines (54 loc) · 2.31 KB
/
Copy pathfindWiddle.js
File metadata and controls
65 lines (54 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const words = require('./filteredWords.js');
/* A Widdle is a set of 5 words that cover nearly all letters of the alphabet with minimal repeats.
Using these five words in a Wordle will rule out most letters, making the final guess easy.
This is a fairly basic recursive algorithm and could absolutely use some work. Feel free to fiddle around.*/
const tolerance = 2; // Number between 0 - 5. Tolerance allows for the final word to have this number of repeated letters. If you get a maximum stack error then raise this number.
const startIndex = 0; // Number between 0 - 8013. Change this to get a different starting word. Higher numbers will eventually produce worse results.
const widdleLength = 5; // Number between 1 - 5. Not to be over 5, something might explode.
const widdle = [];
const usedLetters = [];
function addToWiddle(word, idx) {
widdle.push([word, idx])
word.split('').forEach(letter => { usedLetters.push(letter); });
}
function findNextWord(index){
return words.slice(index).find(word => {
const hasForbiddenLetters = word.split('').some( letter => usedLetters.includes(letter));
return !hasForbiddenLetters;
})
}
function recurse(widdle, index) {
if (widdle.length === widdleLength ) {
return;
} else {
let candidate;
if (widdle.length === widdleLength - 1) { // Special case allowing tolerance for the final word
candidate = words.slice(index).find(word => {
let count = 0;
const hasForbiddenLetters = word.split('').some( letter => {
if (usedLetters.includes(letter)) {
count++;
if (count >= tolerance) { return true; }
}
return false;
});
return !hasForbiddenLetters;
})
} else {
candidate = findNextWord(index);
};
if (candidate === undefined) {
idx = widdle[widdle.length-1][1]+1;
widdle.pop();
usedLetters.splice(usedLetters.length-5, 5);
} else {
idx = words.indexOf(candidate);
addToWiddle(candidate, idx);
}
recurse(widdle, idx);
}
}
addToWiddle(words[startIndex],startIndex);
recurse(widdle, startIndex);
console.log(widdle);
return widdle;