Created
February 14, 2022 16:27
-
-
Save andyreagan/bd1f92f71e1539499edf5d95a9b47ab1 to your computer and use it in GitHub Desktop.
Obligatory wordle coding
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
from math import prod | |
from itertools import product | |
import click | |
LETTERS = "abcdefghijklmnopqrstuvwxyz" | |
assert len(LETTERS) == 26 | |
def get_all_possibilities(guesses: list, results: list) -> list: | |
bad_letters_global = [ | |
g[i] | |
for i in range(5) | |
for g, r in zip(guesses, results) | |
if r[i] in {"x"} and sum([g[j] == g[i] for j in range(5) if r[j] != "o"]) == 1 | |
] | |
need_letters_global = [ | |
g[i] for i in range(5) for g, r in zip(guesses, results) if r[i] in {"-"} | |
] | |
bad_letters_position = [ | |
[g[i] for g, r in zip(guesses, results) if r[i] in {"x", "-"}] for i in range(5) | |
] | |
correct_letters_position = [ | |
[g[i] for g, r in zip(guesses, results) if r[i] in {"o"}] for i in range(5) | |
] | |
remaining_letters_global = [x for x in LETTERS if x not in bad_letters_global] | |
remaining_letters_positions = [ | |
correct_letters_position[i] | |
if len(correct_letters_position[i]) == 1 | |
else [x for x in remaining_letters_global if x not in bad_letters_position[i]] | |
for i in range(5) | |
] | |
n_possibilities = prod([len(x) for x in remaining_letters_positions]) | |
if n_possibilities < 10000: | |
return ["".join(x) for x in list(product(*remaining_letters_positions))] | |
else: | |
return [f"{n_possibilities=} too many! "] | |
@click.command() | |
@click.argument("guesses") | |
@click.argument("results") | |
def cli(guesses, results): | |
"""Print all possible guesses. | |
GUESSES is a comma separated list of guesses, lower case. | |
RESULTS is a comma separated list of results in x-o format: | |
* x for wrong. | |
* - for in word but wrong position. | |
* o for correct. | |
""" | |
all_possibilities = get_all_possibilities(guesses, results) | |
for x in all_possibilities: | |
print(x) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment