-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquabble.java
More file actions
182 lines (162 loc) · 6.54 KB
/
Squabble.java
File metadata and controls
182 lines (162 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class Squabble {
private static class WordScore {
public WordScore(String w, int s) {
this.word = w;
this.score = s;
}
public String word;
public int score;
public int getScore() {
return this.score;
}
public String toString() {
return "(" + this.word + " -> " + this.score + ")";
}
}
private static Logger logger = Logger.getGlobal();
private static Set<String> wordsOf5;
private static int[] charFreqs = new int[26];
private static List<WordScore> wordScores = new ArrayList<>();
private static Map<Integer, Map<Integer, List<WordScore>>> byPositions = new HashMap<>();
private static int[] charState = new int[26];
private static boolean distinctChars(String s) {
return s.length() == s.chars().distinct().count();
}
private static boolean containsChar(String s, char c) {
return s.indexOf(c) != -1;
}
private static void getWordsOf5() throws IOException {
wordsOf5 = Files.lines(Paths.get("/usr/share/dict/words"))
.filter(s -> s.length() == 5)
.map(String::toLowerCase)
// .filter(s -> distinctChars(s))
.collect(Collectors.toSet());
logger.info("Read " + wordsOf5.size() + " words...");
}
private static void populateFreqs() {
wordsOf5.forEach(x -> {
x.chars().forEach(c -> charFreqs[c - 'a']++);
});
wordsOf5.forEach(w ->
wordScores.add(
new WordScore(
w,
w.chars().map(c -> charFreqs[c - 'a']).sum())
)
);
wordScores.sort(Comparator.comparing(WordScore::getScore).reversed());
}
private static void populatePositionMap() {
for(int i = 0; i < 26; ++i) {
byPositions.put(i, new HashMap<>());
for(int j = 0; j < 5; ++j) {
byPositions.get(i).put(j, new ArrayList<>());
}
}
wordScores.forEach(w -> {
w.word.chars().forEach(c -> {
byPositions.get(c - 'a')
.get(w.word.indexOf(c, 0))
.add(w);
});
});
}
private static List<WordScore> popularWords(int n) {
return wordScores.stream().limit(n).collect(Collectors.toList());
}
private static WordScore randomPopularWord() {
final int range = 100;
return popularWords(range).get((int)Math.floor(Math.random() * range) - 1);
}
private static Set<String> guessBestNextWords(final String currentWord, final String hint) {
// utilize hint to guess next best next words with score. B is still BLANK, G is GREEN, Y is YELLOW
if(currentWord.isEmpty() || hint.isEmpty()) {
return Set.of(randomPopularWord().word);
} else {
List<Integer> greenIndexes = new ArrayList<>();
List<Integer> yellowIndexes = new ArrayList<>();
// cross-out all blanks in charState
for(int i = 0; i < 5; ++i) {
if(hint.charAt(i) == 'b') {
final char c = currentWord.charAt(i);
wordsOf5.removeIf(w -> containsChar(w, c));
} else if(hint.charAt(i) == 'g') {
greenIndexes.add(i);
} else if(hint.charAt(i) == 'y') {
yellowIndexes.add(i);
}
}
logger.info("Remaining " + wordsOf5.size() + " words after blank removal!");
// find all green words at position
Set<String> greens = new HashSet<>();
for(int gIndex: greenIndexes) {
Set<String> t = byPositions.get(currentWord.charAt(gIndex) - 'a')
.get(gIndex).stream()
.map(g -> g.word)
.collect(Collectors.toSet());
greens.addAll(t);
}
StringBuilder regex = new StringBuilder();
for(int i = 0; i < 5; i++) {
if(greenIndexes.contains(i)) regex.append(currentWord.charAt(i));
else regex.append(".");
}
final Pattern pattern = Pattern.compile(regex.toString());
greens.removeIf(g -> {
Matcher matcher = pattern.matcher(g);
if(matcher.find()) return false;
else return true;
});
// update master state again
if(!greens.isEmpty()) wordsOf5.retainAll(greens);
logger.info("Remaining " + wordsOf5.size() + " words after green inclusion!");
// make sure they have yellows
Set<Character> yellows = yellowIndexes.stream()
.map(i -> currentWord.charAt(i))
.collect(Collectors.toSet());
wordsOf5.removeIf(w -> {
boolean remove = false;
for(char c : yellows) {
if(containsChar(w, c)) continue;
else return true;
}
return remove;
});
logger.info("Remaining " + wordsOf5.size() + " words after yellow consideration!");
}
return wordsOf5;
}
public static void main(String[] args) {
try {
getWordsOf5();
} catch(final Exception e) {
logger.log(Level.SEVERE, "failed to laod dict", e);
}
populateFreqs();
populatePositionMap();
String initialState = "";
String hint = "";
String currentInput = "";
System.out.println(guessBestNextWords(initialState.toLowerCase(), hint.toLowerCase()));
while(true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter current input: ");
currentInput = scanner.nextLine();
System.out.println("Enter hint: ");
hint = scanner.nextLine();
System.out.println(guessBestNextWords(currentInput.toLowerCase(), hint.toLowerCase()));
}
}
}