Skip to content

Instantly share code, notes, and snippets.

@charmoniumQ
Created April 1, 2023 21:32
Show Gist options
  • Save charmoniumQ/2d60af77d0c2142ca1a99aad7ecf0980 to your computer and use it in GitHub Desktop.
Save charmoniumQ/2d60af77d0c2142ca1a99aad7ecf0980 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import requests
import lxml.etree
import tqdm
import math
import heapq
import numpy
import random
xml_text = requests.get("http://www.remush.be/tezauro/PIV.xml").content
xml_text = xml_text.replace(b"encoding=\"UTF-16\"", b"encoding=\"UTF-8\"")
words = [
word.text.strip()
for word in lxml.etree.fromstring(xml_text)
]
print(f"{len(words)} esperanto words")
wordle_words = [word for word in words if len(word) == 5 and word == word.lower()]
print(f"{len(wordle_words)} wordle esperanto words")
def wordle_filter(words, green_letters, yellow_letters, gray_letters):
return [
word
for word in words
if all([
all(letter == word[i] for (i, letter) in green_letters),
all(letter != word[i] for (i, letter) in yellow_letters),
all(letter in word for (_, letter) in yellow_letters),
all(letter not in word for letter in gray_letters),
])
]
def wordle_entropy(words, guess):
entropy = 0
for letter in set(guess):
selected_words = sum(letter in word for word in words)
p = selected_words / len(words)
entropy += -p * (math.log(p) / math.log(2))
return entropy
def wordle_best_guesses(words, n, quiet = False):
return heapq.nlargest(
min(n, len(words)),
tqdm.tqdm(words, disable=quiet),
key=lambda guess: wordle_entropy(words, guess),
)
candidates = wordle_words
for guess in wordle_best_guesses(candidates, 20):
print(guess, wordle_entropy(wordle_words, guess))
# If you want a hint, fill in the letters of your wordle game here.
candidates = wordle_filter(
wordle_words,
green_letters=((0, "l"), (1, "a"), (2, "ŭ"), (4, "o")),
yellow_letters=(),
gray_letters="erniutrkt",
)
for guess in wordle_best_guesses(candidates, 20):
print(guess, wordle_entropy(wordle_words, guess))
# These functions are for simulating an entire game.
def wordle_grade_guess(guess, answer):
if len(guess) != len(answer):
raise RuntimeError(f"Guess must be {len(answer)} long")
green = []
yellow = []
gray = []
for i, letter in enumerate(guess):
if letter == answer[i]:
green.append((i, letter))
elif letter in answer:
yellow.append((i, letter))
else:
gray.append(letter)
return green, yellow, gray
def wordle_play(wordle_words):
answer = random.choice(wordle_words)
candidates = wordle_words
guess = None
guesses = 0
green = set()
yellow = set()
gray = set()
while guess != answer:
guess = wordle_best_guesses(candidates, 1, quiet=True)[0]
this_green, this_yellow, this_gray = wordle_grade_guess(guess, answer)
green |= set(this_green)
yellow |= set(this_yellow)
gray |= set(this_gray)
# print(len(green), len(yellow), len(gray), guess)
candidates = wordle_filter(wordle_words, green, yellow, gray)
guesses += 1
return guesses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment