Last active
January 28, 2022 07:12
-
-
Save SiestaMadokaist/9dc643f890ff1e54e1d2e64a2cb95b81 to your computer and use it in GitHub Desktop.
Listing Possible words from wordle https://www.powerlanguage.co.uk/wordle/
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
interface IGuess { | |
word: string; | |
score: string; | |
}; | |
const A = 'a'.charCodeAt(0); | |
export class Wordle { | |
/** | |
* 0 = possible | |
* 1 = impossible | |
* 2 = definitely somewhere | |
* 3 = definitiely in this index | |
*/ | |
possibilityLevel: [number[], number[], number[], number[], number[]]; | |
isPossiblyUsed: boolean[]; | |
level2: number[]; | |
level3: Array<{ charId: number, i: number }>; | |
constructor(private guesses: IGuess[]) { | |
this.possibilityLevel = [[], [], [], [], []]; | |
this.isPossiblyUsed = []; | |
this.level2 = []; | |
this.level3 = []; | |
for (let c = 0; c < 26; c++) { | |
for (let i = 0; i < 5; i++) { | |
this.possibilityLevel[i].push(1); | |
} | |
this.isPossiblyUsed.push(true); | |
} | |
this.initialize() | |
} | |
annihilate(charId: number): void { | |
for (let i = 0; i < 5; i++) { | |
this.possibilityLevel[i][charId] = 0; | |
} | |
} | |
initialize(): void { | |
for (const guess of this.guesses) { | |
const { word, score } = guess; | |
for (let i = 0; i < word.length; i++) { | |
const charId = word.charCodeAt(i) - A; | |
const charScore = score[i]; | |
if (charScore === '1') { | |
this.annihilate(charId); | |
console.log(`removed: ${charId} from: ${word}, score: ${score}`); | |
this.isPossiblyUsed[charId] = false; | |
} else if (charScore === '2') { | |
this.possibilityLevel[i][charId] = 0; | |
this.level2.push(charId); | |
} else if (charScore === '3') { | |
this.annihilate(charId); | |
this.possibilityLevel[i][charId] = 2; | |
this.level2.push(charId); | |
this.level3.push({ charId, i }); | |
} | |
} | |
} | |
} | |
isImpossible(index: number, charId: number): boolean { | |
// console.log(0); | |
if (this.isPossiblyUsed[charId] === false) { return true; } | |
// console.log(1); | |
if (this.possibilityLevel[index][charId] === 0) { return true; } | |
// console.log(2); | |
return false; | |
} | |
isPossibleWord(ascii: number[]) { | |
for (const char of this.level2) { | |
if (ascii.indexOf(char) === -1) { return false; } | |
} | |
for (const v of this.level3) { | |
if (ascii[v.i] !== v.charId) { return false; } | |
} | |
return true; | |
} | |
createGuess(): void { | |
let count = 0; | |
for (let c0 = 0; c0 < 26; c0++) { | |
if (this.isImpossible(0, c0)) { continue; } | |
for (let c1 = 0; c1 < 26; c1++) { | |
if (this.isImpossible(1, c1)) { continue; } | |
for (let c2 = 0; c2 < 26; c2++) { | |
if (this.isImpossible(2, c2)) { continue; } | |
for (let c3 = 0; c3 < 26; c3++) { | |
if (this.isImpossible(3, c3)) { continue; } | |
for (let c4 = 0; c4 < 26; c4++) { | |
if (this.isImpossible(4, c4)) { continue; } | |
const ascii = [c0, c1, c2, c3, c4]; | |
if (!this.isPossibleWord(ascii)) { continue; } | |
const word = ascii | |
.map((x) => x + A) | |
.map((i) => String.fromCharCode(i)) | |
.join(''); | |
count++; | |
console.log(count, word); | |
} | |
} | |
} | |
} | |
} | |
} | |
fromWord(word: string): number[] { | |
return word.split('').map((x) => x.charCodeAt(0) - A); | |
} | |
} | |
function main(): void { | |
const guess: IGuess[] = [ | |
{ word: 'weary', score: '13123' }, | |
{ word: 'close', score: '11112' }, | |
{ word: 'mound', score: '11111' } | |
]; | |
const w = new Wordle(guess); | |
w.createGuess(); | |
} | |
if (process.argv[1] === __filename) { | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment