ChiaraStellata

Wordle Solver

Jan 12th, 2022 (edited)
1,324
0
Never
14
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. # Download word list from https://www-cs-faculty.stanford.edu/~knuth/sgb-words.txt
  2.  
  3. import random
  4. import sys
  5. from collections import defaultdict
  6.  
  7. def get_wordle_hints(guess, answer):
  8.     result = ''
  9.     answer_minus_green = ''
  10.     for i in range(len(guess)):
  11.         if guess[i] == answer[i]:
  12.             answer_minus_green += ' '
  13.         else:
  14.             answer_minus_green += answer[i]
  15.    
  16.     for i in range(len(guess)):
  17.         if guess[i] == answer[i]:
  18.             result += 'g'
  19.         elif guess[i] in answer_minus_green:
  20.             result += 'y'
  21.             pos = answer_minus_green.find(guess[i])
  22.             answer_minus_green = answer_minus_green[0:pos] + ' ' + answer_minus_green[pos+1:]
  23.         else:
  24.             result += 'k'
  25.     return result
  26.  
  27. def get_best_guesses(words, guesses_so_far):
  28.     valid_words = []
  29.     for word in words:
  30.         success = True
  31.         for guess in guesses_so_far:
  32.             if get_wordle_hints(guess, word) != guesses_so_far[guess]:
  33.                 success = False
  34.         if success:
  35.             valid_words.append(word)
  36.  
  37.     if len(valid_words) == 0: # Can only happen with invalid hints
  38.         return [('xxxxx', 0)]
  39.     if len(valid_words) == 1:
  40.         return [(valid_words[0], 0)]
  41.  
  42.     guess_ratings = dict()
  43.     # for guess in valid_words: # HARD MODE
  44.     counter = 0
  45.     for guess in words:
  46.         counter += 1
  47.         # if (counter % 100 == 0):
  48.         #     print(counter)
  49.         hints_dict = defaultdict(int)
  50.         for answer in valid_words:
  51.             hints_dict[get_wordle_hints(guess, answer)] += 1
  52.         guess_ratings[guess] = max(hints_dict.values())
  53.    
  54.     sorted_guess_ratings = sorted(guess_ratings.items(), key=lambda item: item[1])
  55.     best_rating = sorted_guess_ratings[0][1]
  56.     best_guesses = [x for x in sorted_guess_ratings if x[1] == best_rating]
  57.     best_guesses_in_valid_words = [x for x in best_guesses if x[0] in valid_words]
  58.     if len(best_guesses_in_valid_words) > 0:
  59.         return best_guesses_in_valid_words
  60.     else:
  61.         return best_guesses
  62.  
  63. words = list()
  64. with open('sgb-words.txt') as f:
  65.     for line in f:
  66.         if len(line.strip()) == 5:
  67.             words.append(line.strip())
  68.  
  69. first_guess = 'aloes'
  70.  
  71. guess = first_guess
  72. guesses_so_far = dict()
  73. while True:
  74.     print('Next guess: ' + guess)
  75.     hint = input('Enter result (k for black, y for yellow, g for green): ')
  76.     guesses_so_far[guess] = hint
  77.     best_guesses = get_best_guesses(words, guesses_so_far)
  78.     guess = best_guesses[0][0]
  79.     if guess == 'xxxxx':
  80.         print('Word is not in dictionary.')
  81.         break
  82.     elif best_guesses[0][1] == 0:
  83.         print('The word is: ' + guess)
  84.         break
  85.    
  86. sys.exit(0)
  87.  
  88. # The below code is used to test the solver on all the words in the dictionary and see how it does
  89.  
  90. print("Building second_guess_dict...")
  91.  
  92. second_guess_dict = dict()
  93. for c1 in ['k', 'y', 'g']:
  94.     for c2 in ['k', 'y', 'g']:
  95.         for c3 in ['k', 'y', 'g']:
  96.             for c4 in ['k', 'y', 'g']:
  97.                 for c5 in ['k', 'y', 'g']:
  98.                     hint = c1 + c2 + c3 + c4 + c5
  99.                     best_guess = get_best_guesses(words, {first_guess: hint})[0][0]
  100.                     second_guess_dict[hint] = best_guess
  101.                     print(hint + ' ' + best_guess)
  102.  
  103. num_guesses_dict = defaultdict(int)
  104.  
  105. counter = 0
  106. for word in words:
  107.     counter += 1
  108.     if (counter % 100) == 0:
  109.         print(counter)
  110.     guess = first_guess
  111.     guesses_so_far = dict()
  112.     # print(word)
  113.     if guess != word:
  114.         hint = get_wordle_hints(guess, word)
  115.         guesses_so_far[guess] = hint
  116.         # print(guess + ' ' + hint)
  117.         guess = second_guess_dict[hint]
  118.     while guess != word:
  119.         hint = get_wordle_hints(guess, word)
  120.         guesses_so_far[guess] = hint
  121.         # print(guess + ' ' + hint)
  122.         best_guesses = get_best_guesses(words, guesses_so_far)
  123.         # guess = best_guesses[random.randint(0,len(best_guesses)-1)][0]
  124.         guess = best_guesses[0][0]
  125.     num_guesses = len(guesses_so_far) + 1
  126.     num_guesses_dict[num_guesses] += 1
  127.     # print(word + ' ' + str(num_guesses))
  128.     if num_guesses > 6:
  129.         print(word + ' ' + str(num_guesses))
  130.  
  131. for k, v in sorted(num_guesses_dict.items(), key=lambda item: item[0]):
  132.     print(str(k) + ': ' + str(v))
  133. print(float(sum([k*v for k, v in num_guesses_dict.items()])) / sum([v for k, v in num_guesses_dict.items()]))
  134.  
Advertisement
Comments
  • unknown_SDVY
    172 days
    Comment was deleted
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment