-
-
Save KateLibC/0838436b3284a150b734f870364f462f to your computer and use it in GitHub Desktop.
Wordle Guesser
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
#!/usr/bin/env python3 | |
import re | |
import sys | |
wildcard_char = '_' | |
starting_keep_char = '.' | |
wordfile = sys.argv[1] | |
ignore_letters = sys.argv[2].lower() if sys.argv[2] != '_' else '' | |
require_letters = sys.argv[3].lower() if sys.argv[3] != '_' else '' | |
letter_positions = {} | |
first_pass = [] | |
second_pass = [] | |
''' | |
Sets the position of the letters and forces lower case | |
''' | |
n = 0 | |
for c in sys.argv[4:]: | |
letter_positions[n] = c.lower() | |
n += 1 | |
''' | |
Generates a regular expression based on inputs | |
''' | |
def generate_ignore(gi, li=None): | |
i = gi + li if li is not None else gi | |
i = ''.join(sorted(dict([(x, None) for x in i if x not in [wildcard_char, starting_keep_char]]).keys())) | |
return f'[^{i}]' | |
''' | |
Outputs a word file and forces all to lowercase separated by a newline | |
''' | |
def wordlist(file_in): | |
with open(file_in, 'r') as f: | |
return [x.replace('\n', '').lower() for x in f.readlines()] | |
''' | |
Boolean output from a word and regex | |
''' | |
def wordmatch(word, r): | |
return bool(re.search(r, word)) | |
if __name__ == '__main__': | |
gen_regex = '^' | |
for p, l in letter_positions.items(): | |
if len(l) == 1: | |
if l == wildcard_char: | |
gen_regex += generate_ignore(gi=ignore_letters) | |
else: | |
gen_regex += l | |
else: | |
if l[0] == starting_keep_char: | |
gen_regex += generate_ignore(gi=ignore_letters, li=l[1:]) | |
for word in wordlist(file_in=wordfile): | |
if wordmatch(word=word, r=gen_regex): | |
first_pass.append(word) | |
for word in first_pass: | |
w = [] | |
for l in require_letters: | |
if l in word: | |
w.append(l) | |
if len(w) >= len(require_letters): | |
second_pass.append(word) | |
print(second_pass[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment