wordle/solve.py

150 lines
4.9 KiB
Python
Raw Normal View History

2022-01-17 11:24:32 +01:00
import random
from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, DEBUG
def remove_non_letters(word, non_letters):
for non_letter in non_letters:
if non_letter in word:
return True
return False
def remove_positional_letters(word, positional_letters):
for pos_idx, positional_letter in enumerate(positional_letters):
if positional_letter == BLANK:
continue
if positional_letter not in word:
return True
if word[pos_idx] != positional_letter:
return True
return False
def remove_non_positional_letters(word, non_positional_letters):
for non_pos_idx, non_positional_letter in enumerate(non_positional_letters):
if non_positional_letter == BLANK:
continue
if non_positional_letter not in word:
return True
if word[non_pos_idx] == non_positional_letter:
return True
return False
def round_words(non_letters, all_positional_letters, all_non_positional_letters):
for word in cat_words():
skip = remove_non_letters(word, non_letters)
if skip:
continue
# Find positional letters, e.g. a----
for positional_letters in all_positional_letters:
skip = remove_positional_letters(word, positional_letters)
if skip:
break
if skip:
continue
# Find non-positional letters, e.g. h-s--
for non_positional_letters in all_non_positional_letters:
skip = remove_non_positional_letters(word, non_positional_letters)
if skip:
break
if skip:
continue
if not skip:
yield word
def first_round_words():
for word in cat_words():
word_break = [letter for letter in word]
skip = False
for letter in word_break:
if word_break.count(letter) > 1:
skip = True
break
if skip:
continue
yield word
def resolve_positions(letters):
positional_letters, non_positional_letters, is_good = '', '', True
for letter in letters:
if letter == BLANK:
positional_letters += BLANK
non_positional_letters += BLANK
elif letter.isupper():
positional_letters += letter.lower()
non_positional_letters += BLANK
elif letter.islower():
positional_letters += BLANK
non_positional_letters += letter
else:
print("ERROR: {}".format(INSTRUCTIONS))
is_good = False
return positional_letters, non_positional_letters, is_good
def main_loop():
all_non_letters = ''
all_positional_letters = []
all_non_positional_letters = []
print(INSTRUCTIONS)
for round_number in range(ROUNDS):
if round_number == 0:
this_round_words = [w for w in first_round_words()]
else:
this_round_words = [w for w in
round_words(all_non_letters, all_positional_letters, all_non_positional_letters)]
if DEBUG:
for word in this_round_words:
print(word)
print("This round words: {}".format(len(this_round_words)))
print("=" * 10)
if len(this_round_words) == 0:
print("ERROR: No known words left.")
break
if DEBUG:
print("ALL Non-letters: {}".format(','.join(all_non_letters)))
print("ALL Positional letters: {}".format(','.join(all_positional_letters)))
print("ALL Non-Positional letters: {}".format(','.join(all_non_positional_letters)))
print("Random word: {}".format(random.choice(this_round_words)))
if len(this_round_words) == 1:
print("That's all I got.")
break
print("=" * 10)
print("ROUND {}".format(round_number + 1))
print("-" * 10)
while True:
non_letters = input("Non letters: ")
if not all([x.islower() for x in non_letters]):
print("ERROR: {}".format(INSTRUCTIONS))
continue
break
all_non_letters += non_letters
while True:
letters = input("Letters: ")
if len(letters) != 5:
print("ERROR: {}".format(INSTRUCTIONS))
continue
positional_letters, non_positional_letters, is_good = resolve_positions(letters)
if is_good:
all_positional_letters.append(positional_letters)
all_non_positional_letters.append(non_positional_letters)
break
if all([letter.isupper for letter in letters]):
print("WIN")
else:
print("Sorry. Remaining words: {}".format(len(this_round_words)))
for word in this_round_words:
print(word)
def main():
main_loop()
if __name__ == "__main__":
main()