-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSecretDetective.java
More file actions
286 lines (237 loc) · 7.56 KB
/
SecretDetective.java
File metadata and controls
286 lines (237 loc) · 7.56 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
/**
* <pre>
* A lot of the other solutions used topological sorting algorithms.
*
* My approach uses a scoring method and adjusts the scores accordingly,
* shifts values up/down, and then finally sorts the scores.
* For longer lengths, SCORE_INC will need to be increased.
*
* In the worst case, this is probably O(NN) I think lol, but in the best case,
* it is probably faster than the other solutions that I looked at.
*
* Later, I added #makeSecret(...) for fun.
* </pre>
*
* @author Bradley Whited
* @see https://www.codewars.com/kata/recover-a-secret-string-from-random-triplets/java
* @rank 4 kyu
*/
public class SecretDetective {
public static final int SCORE_INC = 1000;
private Map<Character,Letter> map = new HashMap<>();
private int nextScore = 0;
public static void main(String[] args) {
SecretDetective sd = new SecretDetective();
char[][] triplets = {
{'t','u','p'},
{'w','h','i'},
{'t','s','u'},
{'a','t','s'},
{'h','a','p'},
{'t','i','s'},
{'w','h','s'}
};
// whatisup
System.out.println(sd.recoverSecret(triplets));
triplets = new char[][]{
{'c','d','e'},
{'a','b','c'}
};
// abcde
System.out.println(sd.recoverSecret(triplets));
// Make a secret
triplets = sd.makeSecret("monkey burger");
System.out.println('[');
for(char[] triplet: triplets) {
System.out.println(" " + Arrays.toString(triplet));
}
System.out.println(']');
System.out.println("" + triplets.length + ": " + sd.recoverSecret(triplets));
}
public String recoverSecret(char[][] triplets) {
return recoverSecret(triplets,true);
}
public String recoverSecret(char[][] triplets,boolean showScores) {
clear();
for(char[] triplet: triplets) {
for(int i = 0; i < triplet.length; ++i) {
char value = triplet[i];
Letter letter = getLetterOrDefault(value);
letter.addNeighbors(triplet,i);
letter.checkNeighbors();
}
}
if(showScores) {
map.values().stream().sorted()
.forEach((l) -> System.out.print(l + "[" + l.score + "] "));
System.out.println();
}
return map.values()
.stream()
.sorted()
.map(Object::toString)
.collect(Collectors.joining());
}
/**
* <pre>
* I made this method afterward for fun.
* It is probably pretty inefficient.
*
* I originally did this idea in Ruby, which was far less code.
* It looks kind of weird in Java.
* </pre>
*/
public char[][] makeSecret(String secret) {
List<Letter> letters = new ArrayList<>(secret.length());
Random rand = new Random();
char[][] result = null;
List<char[]> triplets = new ArrayList<>();
// Get rid of duplicate chars
Set<Character> dups = new HashSet<>();
StringBuilder newSecret = new StringBuilder();
for(int i = 0; i < secret.length(); ++i) {
char c = secret.charAt(i);
if(!dups.contains(c)) {
dups.add(c);
newSecret.append(c);
}
}
secret = newSecret.toString();
if(secret.length() < 3) { throw new RuntimeException("Too few chars"); }
// Convert secret to Letters for the scores
clear(); // Reset #nextScore
for(int i = 0; i < secret.length(); ++i) {
letters.add(new Letter(secret.charAt(i)));
}
while(true) {
List<Letter> sampleLetters = new ArrayList<>(letters);
char[] triplet = new char[3];
Letter[] tripletLetters = new Letter[3];
// Take random samples and sort
for(int i = 0; i < 3; ++i) {
tripletLetters[i] = sampleLetters.remove(rand.nextInt(sampleLetters.size()));
}
Arrays.sort(tripletLetters);
// Convert sorted samples to char[] (triplet) and add to triplets
for(int i = 0; i < 3; ++i) {
triplet[i] = tripletLetters[i].value;
}
triplets.add(triplet);
// Test these triplets to see if they work
result = triplets.toArray(new char[triplets.size()][3]);
if(recoverSecret(result,false).equals(secret)) { break; }
}
return result;
}
private Letter getLetterOrDefault(char value) {
Letter letter = map.get(value);
// If-statement instead of Map#putIfAbsent(...) so that #nextScore() isn't called needlessly
if(letter == null) { map.put(value,letter = new Letter(value)); }
return letter;
}
private int nextScore() {
return nextScore += SCORE_INC;
}
public void clear() {
map.clear();
nextScore = 0;
}
private class Letter implements Comparable<Letter> {
private Set<Letter> lows = new HashSet<Letter>(),highs = new HashSet<Letter>();
private int score;
private char value;
public Letter(char value) {
this.score = nextScore();
this.value = value;
}
public void addNeighbors(char[] triplet,int index) {
addNeighbors(triplet,index,-1,lows); // Low neighbors
addNeighbors(triplet,index,1,highs); // High neighbors
}
public void addNeighbors(char[] triplet,int index,int step,Set<Letter> neighbors) {
for(index += step; index >= 0 && index < triplet.length; index += step) {
char value = triplet[index];
Letter neighbor = getLetterOrDefault(value);
neighbors.add(neighbor);
}
}
public void checkNeighbors() {
checkNeighbors(-1); // Low neighbors
checkNeighbors(1); // High neighbors
}
public void checkNeighbors(int which) {
if(which == 0) { return; }
Set<Letter> main = (which < 0) ? lows : highs;
Set<Letter> sub = (which < 0) ? highs : lows;
boolean shift = false;
for(Letter m: main) {
// Is the score wrong and should be higher or lower?
if((which < 0 && score <= m.score) ||
(which > 0 && score >= m.score)) {
score = (which < 0) ? (m.score + (SCORE_INC / 2)) : (m.score / 2);
shift = true;
}
// Shift highs up or lows down for new adjusted score
if(shift) {
for(Letter s: sub) {
s.checkNeighbors(which);
}
}
}
}
// This was my original solution.
// Converted these 2 methods into 1 (slower) method: #checkNeighbors(...).
// I did this so that it'd be easier to modify and understand.
// Leaving the code here for historical purposes.
/*public void checkLows() {
boolean checkHighs = false;
for(Letter low: lows) {
if(score <= low.score) { // Score should be higher
score = low.score + (SCORE_INC / 2);
checkHighs = true;
}
}
if(checkHighs) { // Shift highs up
for(Letter high: highs) {
high.checkLows();
}
}
}
public void checkHighs() {
boolean checkLows = false;
for(Letter high: highs) {
if(score >= high.score) { // Score should be lower
score = high.score / 2;
checkLows = true;
}
}
if(checkLows) { // Shift lows down
for(Letter low: lows) {
low.checkHighs();
}
}
}*/
public int compareTo(Letter l) {
return score - l.score;
}
public boolean equals(Object o) {
return o instanceof Letter && value == ((Letter)o).value;
}
public int hashCode() {
return Objects.hashCode(value);
}
public String toString() {
return String.valueOf(value);
}
}
}