Skip to content

Instantly share code, notes, and snippets.

@KTibow
Created February 8, 2023 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KTibow/b30bb1e5afe5f02fa165cf64550a8807 to your computer and use it in GitHub Desktop.
Save KTibow/b30bb1e5afe5f02fa165cf64550a8807 to your computer and use it in GitHub Desktop.
words = set(open("words.txt").read().splitlines())
word_length = int(input("Enter word length: "))
words = {word.lower() for word in words if len(word) == word_length}
letters = "abcdefghijklmnopqrstuvwxyz"
def calc_reduction(letter):
masks = ["".join([letter if c == letter else " " for c in word]) for word in words]
counts = dict()
for item in masks:
counts[item] = counts.get(item, 0) + 1
nums_remaining = [remaining**2 for remaining in counts.values()]
est_remaining = sum(nums_remaining) / len(words) / len(words)
return 1 - est_remaining
# def calc_chance_of_use(letter):
# return len([word for word in words if letter in word]) / len(words)
def mask_words(mask, mask_letter):
return {
word
for word in words
if all([(word[i] == mask_letter) ^ (mask[i] == " ") for i in range(len(word))])
}
while True:
if len(words) < 2:
print("Nothing left to guess! Words:", words)
break
print(len(words), "word(s) remaining")
best_letter = max(
letters,
key=lambda letter: calc_reduction(letter),
)
print(
"Try guessing",
best_letter,
f"(it eliminates {calc_reduction(best_letter) * 100:.2f}% of words on average)",
)
new_mask = ""
while len(new_mask) != word_length:
new_mask = input(f"New mask: ")
words = mask_words(new_mask, best_letter)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment