Guest User

wordle_solver

a guest
Jan 11th, 2022
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. package com.cxense.cube;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.Arrays;
  7. import java.util.Comparator;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import java.util.stream.Collectors;
  11.  
  12. public class Wordle {
  13.     static final int WORD_LENGTH = 5;
  14.  
  15.     public static int guess(char[] solution, char[] guess) {
  16.         int response = 0;
  17.         for (int i = 0; i < WORD_LENGTH; i++) {
  18.             response *= 3;
  19.             if (guess[i] == solution[i]) {
  20.                 response += 2;
  21.             } else {
  22.                 int count = 0;
  23.                 for (int j = 0; j < WORD_LENGTH; j++) {
  24.                     if (guess[j] == guess[i]) count--;
  25.                     if (solution[j] == guess[i]) count++;
  26.                 }
  27.                 if (count >= 0) {
  28.                     response++;
  29.                 }
  30.             }
  31.         }
  32.         return response;
  33.     }
  34.  
  35.     public static List<String> readWordList(String path) throws IOException {
  36.         return Files.lines(Paths.get(path))
  37.                 .map(word -> word.toLowerCase(Locale.US))
  38.                 .map(word -> word.replaceAll("[^a-z]", ""))
  39.                 .filter(word -> word.length() == WORD_LENGTH)
  40.                 .collect(Collectors.toList());
  41.     }
  42.  
  43.     static class Solver {
  44.         private List<String> legalWords;
  45.         private List<String> possibleSolutions;
  46.         public Solver(List<String> legalWords, List<String> possibleSolutions) {
  47.             this.legalWords = legalWords;
  48.             this.possibleSolutions = possibleSolutions;
  49.         }
  50.         public boolean hasSolution() {
  51.             return possibleSolutions.size() == 1;
  52.         }
  53.         public String getSolution() {
  54.             return possibleSolutions.get(0);
  55.         }
  56.         public List<String> guess() {
  57.             if (hasSolution()) return Arrays.asList(getSolution());
  58.             char[][] candidateGrid = new char[possibleSolutions.size()][];
  59.             for (int i = 0; i < possibleSolutions.size(); i++) candidateGrid[i] = possibleSolutions.get(i).toCharArray();
  60.             int[] responseHistogram = new int[243]; // 3^5, possible responses
  61.             int[] responses = new int[candidateGrid.length];
  62.             List<String> bestGuesses = legalWords.stream().sorted(Comparator.comparingLong(potentialGuessString -> {
  63.                 char[] potentialGuess = potentialGuessString.toCharArray();
  64.                 Arrays.fill(responseHistogram, 0);
  65.                 for (int i = 0; i < responses.length; i++) {
  66.                     int response = Wordle.guess(candidateGrid[i], potentialGuess);
  67.                     responses[i] = response;
  68.                     responseHistogram[response]++;
  69.                 }
  70.                 int max = 0;
  71.                 for (int i = 0; i < responses.length; i++) {
  72.                     max = Math.max(max, responseHistogram[responses[i]]);
  73.                 }
  74.                 return max;
  75.             })).limit(10).collect(Collectors.toList());
  76.             return bestGuesses;
  77.         }
  78.         public void respond(char[] guess, int response) {
  79.             possibleSolutions = possibleSolutions.stream().filter(word -> Wordle.guess(word.toCharArray(), guess) == response).collect(Collectors.toList());
  80.         }
  81.     }
  82.  
  83.     public static void main(String[] args) throws IOException {
  84.         System.out.println("Starting solve");
  85.         String solution = "query";
  86.         Solver solver = new Solver(
  87.                 readWordList("/Users/andopp/Code/wordle_legal_words.txt"),
  88.                 readWordList("/Users/andopp/Code/wordle_possible_solutions.txt"));
  89.  
  90.         List<String> moves = Arrays.asList("aesir");
  91.  
  92.         int move = 0;
  93.         while (!solver.hasSolution() && move < 20) {
  94.             move++;
  95.             List<String> guesses = move <= moves.size() ? Arrays.asList(moves.get(move - 1)) : solver.guess();
  96.             System.out.println("Guesses: " + guesses.stream().collect(Collectors.joining(" ")));
  97.             String guess = guesses.get(0);
  98.             System.out.println("Move " + move + " considering " + solver.possibleSolutions.size() + " candidates, guessing " + guess);
  99.             solver.respond(guess.toCharArray(), Wordle.guess(solution.toCharArray(), guess.toCharArray()));
  100.         }
  101.         System.out.println("Solution " + solver.getSolution());
  102.     }
  103.  
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment