Created
March 6, 2022 17:37
-
-
Save nauhygon/c984efa6c3fff15854618b2ffa2a9957 to your computer and use it in GitHub Desktop.
Help me, 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
#!/usr/bin/env python | |
import sys | |
# Source: https://github.com/dwyl/english-words/blob/master/words_alpha.txt | |
WORD_FILE="words_alpha.txt" | |
def words_all(word_file=WORD_FILE): | |
lines = open(word_file).readlines() | |
return [l.strip() for l in lines] | |
def words_of_5_letters(word_file=WORD_FILE): | |
return [w for w in words_all(word_file) if len(w) == 5] | |
def choices(guesses, word_file=WORD_FILE): | |
right, elsewhere, wrong = [], [], [] | |
for g in guesses: | |
g = g.lower() | |
enzgs = list(enumerate(zip(g[0::2], g[1::2]))) | |
right += [(c,i) for i,(c,s) in enzgs if s == '+'] | |
elsewhere += [(c,i) for i,(c,s) in enzgs if s == '_'] | |
wrong += [c for i,(c,s) in enzgs if s == '-'] | |
def _right(w): | |
return all([w[i] == c for c,i in right]) | |
def _elsewhere(w): | |
return all([c in w and w[i] != c for c,i in elsewhere]) | |
def _wrong(w): | |
return all([c not in w for c in wrong]) | |
return [w for w in words_of_5_letters() if all([_right(w), _elsewhere(w), _wrong(w)])] | |
if __name__ == "__main__": | |
nargs = len(sys.argv) | |
if nargs <= 1: | |
print("Usage: {} <row1> [<row2> [... <rown>]]".format(sys.argv[0])) | |
print(" <row*> example : A_B+O-V_E- (case insensitive)") | |
print(" X+ (B+ ) : X is in the right position.") | |
print(" X_ (A_ V_) : X is in the word but not in the right place.") | |
print(" X- (O- E-) : X is not in the word.") | |
exit(-1) | |
print(choices(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment