Created
January 26, 2022 07:53
-
-
Save helospark/54c0bafed028a05e8c637f8d21456084 to your computer and use it in GitHub Desktop.
Solves wordle game
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
package com.test; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.nio.channels.Channels; | |
import java.nio.channels.ReadableByteChannel; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.function.Function; | |
public class WordleSolver { | |
public static void main(String[] args) throws IOException { | |
Set<Function<String, Boolean>> predicates = new HashSet<>(); | |
predicates.add(new NumberOfCharacterPredicate(5)); | |
// Add rules based on rounds | |
predicates.add(new ContainsCharacters(new char[] { 'a' })); | |
predicates.add(new DoesNotContainsCharacters(new char[] { 'l', 'n', 'e', 'c', 'i' })); | |
predicates.add(new NotContainsCharAtPosition('a', 0)); | |
predicates.add(new ContainsCharAtPosition('g', 2)); | |
predicates.add(new ContainsCharAtPosition('a', 3)); | |
predicates.add(new ContainsCharAtPosition('r', 4)); | |
File wordlistFile = new File(System.getProperty("user.home"), "wordlist.txt"); | |
if (!wordlistFile.exists() || wordlistFile.length() == 0) { | |
System.out.println("Downloading wordlist..."); | |
URL website = new URL("https://gist.githubusercontent.com/dstrelau/1005478/raw/cdb9b07dd2d0a0bfbcad79731dd07d097aab23b3/wordlist.txt"); | |
ReadableByteChannel rbc = Channels.newChannel(website.openStream()); | |
try (FileOutputStream fos = new FileOutputStream(wordlistFile)) { | |
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); | |
} | |
System.out.println("Download done\n\n"); | |
} | |
if (!wordlistFile.exists() || wordlistFile.length() == 0) { | |
throw new RuntimeException("Unable to get wordlist"); | |
} | |
try (FileInputStream fis = new FileInputStream(wordlistFile)) { | |
String[] lines = new String(fis.readAllBytes()).split("\n"); | |
for (var element : lines) { | |
String lowerElemet = element.toLowerCase(); | |
boolean match = predicates.stream() | |
.map(pred -> pred.apply(lowerElemet)) | |
.allMatch(result -> result == true); | |
if (match) { | |
System.out.println(lowerElemet); | |
} | |
} | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
static class NumberOfCharacterPredicate implements Function<String, Boolean> { | |
int numChar; | |
public NumberOfCharacterPredicate(int numChar) { | |
this.numChar = numChar; | |
} | |
@Override | |
public Boolean apply(String t) { | |
return t.length() == numChar; | |
} | |
} | |
static class ContainsCharacters implements Function<String, Boolean> { | |
ArrayList<String> characters; | |
public ContainsCharacters(char[] chars) { | |
this.characters = new ArrayList<>(); | |
for (var c : chars) { | |
characters.add(c + ""); | |
} | |
} | |
@Override | |
public Boolean apply(String t) { | |
return characters.stream().allMatch(ch -> t.contains(ch)); | |
} | |
} | |
static class DoesNotContainsCharacters implements Function<String, Boolean> { | |
ArrayList<String> characters; | |
public DoesNotContainsCharacters(char[] chars) { | |
this.characters = new ArrayList<>(); | |
for (var c : chars) { | |
characters.add(c + ""); | |
} | |
} | |
@Override | |
public Boolean apply(String t) { | |
return characters.stream().allMatch(ch -> !t.contains(ch)); | |
} | |
} | |
static class ContainsCharAtPosition implements Function<String, Boolean> { | |
char ch; | |
int position; | |
public ContainsCharAtPosition(char ch, int position) { | |
this.ch = ch; | |
this.position = position; | |
} | |
@Override | |
public Boolean apply(String t) { | |
return t.length() > position && t.charAt(position) == ch; | |
} | |
} | |
static class NotContainsCharAtPosition implements Function<String, Boolean> { | |
char ch; | |
int position; | |
public NotContainsCharAtPosition(char ch, int position) { | |
this.ch = ch; | |
this.position = position; | |
} | |
@Override | |
public Boolean apply(String t) { | |
return t.length() > position && t.charAt(position) != ch; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment