Skip to content

Instantly share code, notes, and snippets.

@thygrrr
Last active September 2, 2023 15:28
Show Gist options
  • Save thygrrr/a6a1cdb3fc8ec0013bb692a46542f86b to your computer and use it in GitHub Desktop.
Save thygrrr/a6a1cdb3fc8ec0013bb692a46542f86b to your computer and use it in GitHub Desktop.
Wordle Back-Solver, for final result and scores, how many legal games are there?
# SPDX-License-Identifier: Unlicense OR CC0-1.0+
words = [w.lower().strip() for w in open("words.txt", "r").readlines()]
print("loaded", len(words), "Words")
def match(word: str, final: str, score: chr, index: int) -> bool:
return (score == "G" and word[index] == final[index]) or \
(score == "W" and word[index] not in final) or \
(score == "Y" and word[index] != final[index] and word[index] in final)
def fits(word: str, final: str, scores: str) -> bool:
for i, score in enumerate(scores):
if not match(word, final, score, i):
return False
return True
steps = ["WWWWW", "WWWYY", "WWYYW", "WYWGW", "WWGGG", "GGGGG"]
#steps = ["WGGGG", "WGGGG", "WGGGG", "WGGGG", "GGGGG"]
winner = "fires"
if __name__ == "__main__":
print("playing game with winning word", "'"+winner+"'", "and steps", steps)
legal_games = 1
smart_games = 1
guesses = []
all = set()
for step, pattern in enumerate(steps):
if pattern == "GGGGG":
print(step + 1, pattern, winner)
break
matches = [w for w in words if fits(w, winner, pattern)]
smart_matches = [w for w in matches if w not in guesses]
if smart_matches:
guesses.append(smart_matches[0])
else:
smart_games = 0 # nobody could play like this without a repeat!
unique = [w for w in matches if w not in all]
all.update(matches)
legal_games *= len(matches)
print(step + 1, pattern, len(smart_matches), "sensible candidates out of", len(matches), "with", len(unique), "unique out of", len(matches), matches)
smart_games *= len(smart_matches)
print("legal games:", f'{legal_games:,}')
print("non-stupid games:", f'{smart_games:,}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment