Created
January 11, 2022 16:06
-
-
Save whoeverest/b2f66d69dbc512bf91592b32f7e734a1 to your computer and use it in GitHub Desktop.
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
''' | |
Straregy: for the first guess, find the 5 most common letters and construct | |
a word with those. Most common letters have most chance of revealing _some_ | |
information about the target word. | |
With around 6100 five letter words taken from the Linux wordlist, guessing | |
'esaro' reduces the search space by 5x, on average, so to 1200. Repeating the | |
process 6 times (including constructing a letter frequency counter) will likely | |
yield a result, considering that: 6100 / (5^6) < 1 | |
TODO: apply it recursively :) | |
''' | |
import random | |
from collections import Counter | |
from string import ascii_lowercase | |
legit_five_letter_words = [] | |
with open('/usr/share/dict/words') as f: | |
for word in f: | |
word = word.lower().replace('\n', '') | |
if len(word) != 5: | |
continue | |
no_funny_letters = all(letter in ascii_lowercase for letter in word) | |
if no_funny_letters: | |
legit_five_letter_words.append(word) | |
dictionary = legit_five_letter_words | |
def get_clues(guess, chosen_word): | |
clues = [] | |
for i, letter in enumerate(guess): | |
if letter == chosen_word[i]: | |
clues.append('Y') | |
elif letter in chosen_word: | |
clues.append('P') | |
else: | |
clues.append('.') | |
return ''.join(clues) | |
def filter_by_clue(dictionary, guess, clue): | |
reduced_dictionary = [] | |
for dict_word in dictionary: | |
possible_word = True | |
for i, hint in enumerate(clue): | |
clue_letter = guess[i] | |
if hint == 'Y' and clue_letter != dict_word[i]: | |
possible_word = False | |
elif hint == 'P' and clue_letter not in dict_word: | |
possible_word = False | |
if possible_word: | |
reduced_dictionary.append(dict_word) | |
return reduced_dictionary | |
reductions = [] | |
for _ in range(1000): | |
chosen_word = random.choice(dictionary) | |
guess = 'esaro' | |
clues = get_clues(guess, chosen_word) | |
filtered_dict = filter_by_clue(dictionary, guess, clues) | |
reduction_in_size_percent = 100 * (len(filtered_dict) / len(dictionary)) | |
reductions.append(reduction_in_size_percent) | |
print('average reduction', sum(reductions) / len(reductions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment