Last active
March 19, 2022 05:42
-
-
Save xneg/4dc9f82cb1822c1d7aaef27143558658 to your computer and use it in GitHub Desktop.
Wordle solver using clusters
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
def read_file(file): | |
with open(file) as my_file: | |
words = my_file.readlines() | |
return words | |
def letter_count(word, letter): | |
count = 0 | |
for c in word: | |
if c == letter: | |
count += 1 | |
return count | |
def add_or_append(dict, key): | |
if key not in dict: | |
dict[key] = 1 | |
else: | |
dict[key] += 1 | |
def check_word(test, conceived): | |
result = "" | |
used_letters = {} | |
for i in range(len(conceived)): | |
letter = conceived[i] | |
if letter in test and test[i] != conceived[i]: | |
add_or_append(used_letters, letter) | |
for i in range(len(test)): | |
letter = test[i] | |
if letter not in conceived: | |
result += "0" | |
elif letter == conceived[i]: | |
result += "2" | |
else: | |
if letter in used_letters and used_letters[letter] > 0: | |
result += "1" | |
used_letters[letter] -= 1 | |
else: | |
result += "0" | |
return result | |
assert check_word("насос", "класс") == "01102" | |
assert check_word("сосна", "класс") == "10101" | |
assert check_word("акула", "класс") == "11010" | |
assert check_word("тайна", "труба") == "20002" | |
def get_best_words(words_list): | |
results_vectors = {} | |
for word in words_list: | |
word_results = {} | |
for conceived in words_list: | |
result = check_word(word, conceived) | |
add_or_append(word_results, result) | |
x = sorted(word_results.items(), key=lambda item: item[1], reverse=True)[0:1] | |
results_vectors[word] = x[0][1] | |
return sorted(results_vectors.items(), key=lambda item: item[1], reverse=False) | |
def get_pattern_words(test_word, pattern, words_list): | |
result = [] | |
for word in words_list: | |
if check_word(test_word, word) == pattern: | |
result.append(word) | |
return result | |
def clustered_solution(): | |
# all russian nouns from here https://github.com/Harrix/Russian-Nouns | |
nouns = read_file('russian_nouns.txt') | |
# top russian words by frequency of use from here https://github.com/hingston/russian/blob/master/10000-russian-words-cyrillic-only.txt | |
top_words = read_file('russian_words.txt') | |
top_words = [w.strip() for w in top_words if len(w.strip()) == 5] | |
nouns = [w.strip() for w in nouns if len(w.strip()) == 5] | |
alphabet = set(list("абвгдежзийклмнопрстуфхцчшщъыьэюя")) | |
top_words = [w for w in top_words if set(w) <= alphabet] | |
nouns = [w for w in nouns if set(w) <= alphabet] | |
nouns = set(nouns) | |
top_words = [w for w in top_words if w in nouns] | |
current_word = get_best_words(top_words)[0][0] | |
print(current_word) | |
while True: | |
print("Input match result (0 - gray, 1 - yellow, 2 - green):") | |
pattern = input() | |
top_words = get_pattern_words(current_word, pattern, top_words) | |
current_word = get_best_words(top_words)[0][0] | |
print(current_word) | |
clustered_solution() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment