Created
March 5, 2022 18:21
-
-
Save ddellspe/1186f5661c820b80ebbcbeb938f9a003 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
| import argparse | |
| import re | |
| with open("dictionary.txt", 'r') as f: | |
| WORDS = [word.strip().lower() for word in f.readlines() if len(word.strip()) == 6 and "'" not in word] | |
| def check_wordle(input1, input2): | |
| used_letters = [] | |
| if input1[0] != input2[0]: | |
| return False | |
| used_letters.append(input2[0]) | |
| if input2[1] not in input1 or input2[1] == input1[1]: | |
| return False | |
| used_letters.append(input2[1]) | |
| if input2[2] not in input1 or input2[2] == input1[2] or input2[2] in used_letters: | |
| return False | |
| used_letters.append(input2[2]) | |
| if input2[3] in input1: | |
| return False | |
| used_letters.append(input2[3]) | |
| if input2[4] in input1: | |
| return False | |
| used_letters.append(input2[4]) | |
| if input2[5] not in input1 or input2[5] == input1[5] or input2[5] in used_letters: | |
| return False | |
| return True | |
| def get_candidate_words2(word): | |
| candidates = [] | |
| for candidate in WORDS: | |
| if candidate[0] != word[0]: | |
| continue | |
| letters = set([c for c in candidate]) | |
| first = candidate[0] | |
| sec_letters = set([c for c in candidate[1:]]) | |
| sec_letters.remove(candidate[1]) | |
| second = "".join(sec_letters) | |
| tri_letters = set([c for c in candidate[1:]]) | |
| tri_letters.remove(candidate[2]) | |
| third = "".join(tri_letters) | |
| fourth = "".join(list(letters)) | |
| las_letters = set([c for c in candidate[1:]]) | |
| las_letters.remove(candidate[-1]) | |
| sixth = "".join(las_letters) | |
| regex = re.compile(fr'{first}[{second}][{third}][^{fourth}][^{fourth}][{sixth}]') | |
| if regex.match(word): | |
| if check_wordle(candidate, word): | |
| candidates.append(candidate) | |
| return list(set(candidates)) | |
| def get_candidate_words(word): | |
| letters = set([c for c in word]) | |
| first = word[0] | |
| sec_letters = set([c for c in word[1:]]) | |
| sec_letters.remove(word[1]) | |
| second = "".join(sec_letters) | |
| tri_letters = set([c for c in word[1:]]) | |
| tri_letters.remove(word[2]) | |
| third = "".join(tri_letters) | |
| fourth = "".join(list(letters)) | |
| las_letters = set([c for c in word[1:]]) | |
| las_letters.remove(word[-1]) | |
| sixth = "".join(las_letters) | |
| regex = re.compile(fr'{first}[{second}][{third}][^{fourth}][^{fourth}][{sixth}]') | |
| candidates = [] | |
| for candidate in WORDS: | |
| if regex.match(candidate): | |
| if check_wordle(word, candidate): | |
| candidates.append(candidate) | |
| return candidates | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("word", type=str, nargs='+') | |
| args = parser.parse_args() | |
| for word in args.word: | |
| candidates = get_candidate_words(word) | |
| candidates.extend(get_candidate_words2(word)) | |
| for candidate in candidates: | |
| print(f'{word} {candidate} or {candidate} {word}') | |
| if __name__ == '__main__': | |
| raise SystemExit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment