Created
November 17, 2023 01:10
-
-
Save supermacro/9aafe67bf84c587b82cbebdcfec559ef to your computer and use it in GitHub Desktop.
wordle solver
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
// depends on the words_alpha.txt file from https://github.com/dwyl/english-words | |
import * as readline from 'readline' | |
const file = Bun.file('./words_alpha.txt') | |
const text = await file.text() | |
const wordleWords = text.split('\r\n').filter((w) => w.length === 5) | |
const prompt = (text: string): void => { | |
process.stdout.write(`${text} `) | |
} | |
const showMatches = ( | |
knownLetters: string[], | |
includedLetters: string[], | |
excludedLetters: string[] | |
): void => { | |
const candidates = wordleWords | |
.filter((word) => | |
knownLetters.every((char, idx) => char === '_' || word[idx] === char) && | |
includedLetters.every((char) => word.includes(char)) && | |
!excludedLetters.some((char) => word.includes(char)) | |
) | |
console.log(JSON.stringify(candidates, null, 2)) | |
} | |
function* repl(): Generator<undefined, void, string> { | |
let inputCount = 0 | |
let wordGuess: string = '' | |
let includedLetters: string = '' | |
let excludedLetters: string = '' | |
while (true) { | |
if (inputCount === 0) { | |
prompt('Enter your current guess:') | |
} | |
const input = yield | |
inputCount++ | |
if (inputCount === 1) { | |
if (input.length !== 5) { | |
console.log('must be 5 chars') | |
inputCount = 0 | |
continue | |
} | |
wordGuess = input | |
prompt('Enter other known letters:') | |
} else if (inputCount === 2) { | |
includedLetters = input | |
prompt('Enter letters to be excluded:') | |
} else if (inputCount === 3) { | |
excludedLetters = input | |
showMatches( | |
wordGuess.split(''), | |
includedLetters.split(''), | |
excludedLetters.split('') | |
) | |
console.log('\n') | |
inputCount = 0 | |
} | |
} | |
} | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}) | |
const generator = repl() | |
generator.next() | |
rl.on('line', (input) => { | |
if (input.toLowerCase() === 'exit') { | |
rl.close() | |
} else { | |
generator.next(input) | |
} | |
}) | |
rl.on('close', () => { | |
console.log('Exiting REPL') | |
process.exit(0) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment