Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cxense.cube;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Locale;
- import java.util.stream.Collectors;
- public class Wordle {
- static final int WORD_LENGTH = 5;
- public static int guess(char[] solution, char[] guess) {
- int response = 0;
- for (int i = 0; i < WORD_LENGTH; i++) {
- response *= 3;
- if (guess[i] == solution[i]) {
- response += 2;
- } else {
- int count = 0;
- for (int j = 0; j < WORD_LENGTH; j++) {
- if (guess[j] == guess[i]) count--;
- if (solution[j] == guess[i]) count++;
- }
- if (count >= 0) {
- response++;
- }
- }
- }
- return response;
- }
- public static List<String> readWordList(String path) throws IOException {
- return Files.lines(Paths.get(path))
- .map(word -> word.toLowerCase(Locale.US))
- .map(word -> word.replaceAll("[^a-z]", ""))
- .filter(word -> word.length() == WORD_LENGTH)
- .collect(Collectors.toList());
- }
- static class Solver {
- private List<String> legalWords;
- private List<String> possibleSolutions;
- public Solver(List<String> legalWords, List<String> possibleSolutions) {
- this.legalWords = legalWords;
- this.possibleSolutions = possibleSolutions;
- }
- public boolean hasSolution() {
- return possibleSolutions.size() == 1;
- }
- public String getSolution() {
- return possibleSolutions.get(0);
- }
- public List<String> guess() {
- if (hasSolution()) return Arrays.asList(getSolution());
- char[][] candidateGrid = new char[possibleSolutions.size()][];
- for (int i = 0; i < possibleSolutions.size(); i++) candidateGrid[i] = possibleSolutions.get(i).toCharArray();
- int[] responseHistogram = new int[243]; // 3^5, possible responses
- int[] responses = new int[candidateGrid.length];
- List<String> bestGuesses = legalWords.stream().sorted(Comparator.comparingLong(potentialGuessString -> {
- char[] potentialGuess = potentialGuessString.toCharArray();
- Arrays.fill(responseHistogram, 0);
- for (int i = 0; i < responses.length; i++) {
- int response = Wordle.guess(candidateGrid[i], potentialGuess);
- responses[i] = response;
- responseHistogram[response]++;
- }
- int max = 0;
- for (int i = 0; i < responses.length; i++) {
- max = Math.max(max, responseHistogram[responses[i]]);
- }
- return max;
- })).limit(10).collect(Collectors.toList());
- return bestGuesses;
- }
- public void respond(char[] guess, int response) {
- possibleSolutions = possibleSolutions.stream().filter(word -> Wordle.guess(word.toCharArray(), guess) == response).collect(Collectors.toList());
- }
- }
- public static void main(String[] args) throws IOException {
- System.out.println("Starting solve");
- String solution = "query";
- Solver solver = new Solver(
- readWordList("/Users/andopp/Code/wordle_legal_words.txt"),
- readWordList("/Users/andopp/Code/wordle_possible_solutions.txt"));
- List<String> moves = Arrays.asList("aesir");
- int move = 0;
- while (!solver.hasSolution() && move < 20) {
- move++;
- List<String> guesses = move <= moves.size() ? Arrays.asList(moves.get(move - 1)) : solver.guess();
- System.out.println("Guesses: " + guesses.stream().collect(Collectors.joining(" ")));
- String guess = guesses.get(0);
- System.out.println("Move " + move + " considering " + solver.possibleSolutions.size() + " candidates, guessing " + guess);
- solver.respond(guess.toCharArray(), Wordle.guess(solution.toCharArray(), guess.toCharArray()));
- }
- System.out.println("Solution " + solver.getSolution());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment