-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.php
304 lines (264 loc) · 9.03 KB
/
secret.php
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* This is class contains logic to get secret phrase from anagram
*
* @author Amit Pandey
*/
class Secret {
/**
* MD5 Hash of the secrete phrase
*/
const HASH = '87bb2bda651995d346c05b5049b4d78b';
/**
* @var string $_hint Hint to get the secret phrase
*/
private $_hint;
/**
* @var string $_cleanHint hint string without space
*/
private $_cleanHint;
/**
* @var string $_cleanHintKeyMap key map of the hint string without space
*/
private $_cleanHintKeyMap;
/**
* @var string $_combinationCount count of combinations
*/
private $_combinationCount;
/**
* @var string $_secret the secret
*/
private $_secret = '';
/**
* Constructor
*
* @param string $hint
*/
public function __construct(string $hint) {
$this->_hint = $hint;
$this->_cleanHint = str_replace(' ', '', $this->_hint);
$this->_cleanHintKeyMap = count_chars($this->_cleanHint, 1);
$this->init();
}
/**
* Initialise the class
*
* @return void
*/
public function init() {
// Get all the different characters used in the hint
$pattern = count_chars($this->_cleanHint, 3);
// Get possible words of secret from dictionary using given pattern
$dictWords = $this->_getWordFromDictionary($pattern);
// Arrange the dictonary words length wise
$dictWordsByLen = [];
$dictWordsCountByLen = [];
foreach ($dictWords as $word) {
// Exclude the words if the character count of the word does not match
$patternArr = str_split($pattern);
$hasExtraChar = false;
foreach($patternArr as $hintChar) {
if (substr_count($word, $hintChar) > substr_count($this->_cleanHint, $hintChar)) {
$hasExtraChar = true;
break;
}
}
// Exclude the word if the word contain extra hint chracter
if ($hasExtraChar) {
continue;
}
elseif (!isset($dictWordsByLen[strlen($word)])) {
$dictWordsByLen[strlen($word)] = [];
$dictWordsCountByLen[strlen($word)] = 0;
}
else {
$dictWordsByLen[strlen($word)][] = $word;
$dictWordsCountByLen[strlen($word)] += 1;
}
}
$lengthOfDict = array_keys($dictWordsByLen);
// Array of possible cross joins for secret
$possibleCrossJoins = [];
// Max possible depth of the secret should be total space of the hint + 1
$possibleDepthLevel = substr_count($this->_hint, ' ') + 1;
// Reduce the subset for dictionary length for possible cross joins
$this->_subSetReduce(
$lengthOfDict,
strlen($this->_cleanHint), // Length of clean hint
$possibleCrossJoins,
$possibleDepthLevel);
// Do sorting to possible cross joins to get the lowest combination first.
$possibleCrossJoins = $this->_getSortedCrossJoins(
$possibleCrossJoins,
$dictWordsCountByLen,
$possibleDepthLevel);
// Find the anagrame using possible cross joins
$this->_findAnagram($possibleCrossJoins, $dictWordsByLen);
}
/**
* Return the secret phrase
*
* @return string
*/
public function getSecret() {
return $this->_secret;
}
/**
* Finding anagrams based cross joins from dictionary words
*
* @param array $possibleCrossJoins
* @param array $dictWordsByLen
* @return void
*/
private function _findAnagram($possibleCrossJoins, $dictWordsByLen) {
foreach ($possibleCrossJoins as $possibleCrossJoin) {
$anagrams = [];
foreach ($possibleCrossJoin as $crossJoinValue) {
$anagrams[] = $dictWordsByLen[$crossJoinValue];
}
if ($this->_secret) {
break;
}
$this->_analyseCombination($anagrams);
}
}
/**
* Analyse the anagram combinations to find the secret
*
* @param array $anagrams
* @param int $current
* @param array $temp
* @return void
*/
private function _analyseCombination($anagrams, $current = 0, $temp = []) {
if (empty($temp)) {
$this->_combinationCount = count($anagrams);
}
if (count($temp) == $this->_combinationCount) {
if ($this->_cleanHintKeyMap == count_chars(implode('', $temp), 1)) {
// Rearrange the anagram words in all possible order
$possibleCombination = $this->_getUniqueCombinations($temp);
foreach ($possibleCombination as $possibleSecret) {
if (self::HASH == hash('md5', $possibleSecret)) {
$this->_secret = $possibleSecret;
break;
}
}
}
}
if (!isset($anagrams[$current]) || $current >= $this->_combinationCount) {
return;
}
$count = count($anagrams[$current]);
for ($i = 0; $i < $count; $i++) {
$temp2 = $temp;
$temp2[] = $anagrams[$current][$i];
$this->_analyseCombination($anagrams, $current + 1, $temp2);
}
}
/**
* Return unique possible anagram combinations
*
* @param array $combination
* @param array $temp
* @return array|null
*/
private function _getUniqueCombinations($combination, $temp = []) {
if (empty($temp)) {
$this->_combinationCount = count($combination);
$result = [];
}
elseif (count($temp) == $this->_combinationCount) {
$string = implode(" ", $temp);
return [$string];
}
elseif (count($temp) >= $this->_combinationCount) {
return null;
}
$length = count($combination);
$matches = [];
for ($i = 0; $i < $length; $i++) {
$newTemp = $temp;
$newTemp[] = $combination[$i];
$newCombination = $combination;
unset($newCombination[$i]);
$result = $this->_getUniqueCombinations(array_values($newCombination), $newTemp);
if ($result) {
$matches = array_merge($matches, $result);
}
}
return array_unique($matches);
}
/**
* Return the sorted possible cross joins
*
* @param array $cossJoins
* @param int $counts
* @param int $depth
* @return array
*/
private function _getSortedCrossJoins($cossJoins, $counts, $depth) {
usort($cossJoins, function($first, $next) use($counts) {
$firstCombination = array_reduce($first, function($product, $item) use($counts) {
$product *= $counts[$item];
return $product;
}, 1);
$nextCombination = array_reduce($next, function($product, $item) use($counts) {
$product *= $counts[$item];
return $product;
}, 1);
return $firstCombination - $nextCombination;
});
return array_values(array_filter($cossJoins, function($item) use($depth) {
return count($item) == $depth;
}));
}
/**
* Reduce the subset of dictionary length to get max possible cross joins for secret
*
* @param array $dictLength
* @param int $hintLength
* @param array $combination
* @param int $depth
* @param array $temp
* @return void
*/
private function _subSetReduce($dictLength, $hintLength, &$combination, $depth, $temp = []) {
if (count($temp) > $depth) {
return null;
}
elseif ($hintLength === 0) {
$combination[] = $temp;
return null;
}
elseif ($hintLength <= 0) {
return null;
}
else {
$count = count($dictLength);
for ($i = 0; $i < $count; $i++) {
if ($hintLength < $dictLength[$i]) {
continue;
}
$temp2 = $temp;
$temp2[] = $dictLength[$i];
$this->_subSetReduce(array_slice($dictLength, $i), $hintLength - $dictLength[$i], $combination, $depth, $temp2);
}
}
}
/**
* Return possible words used in secret phrase based on hint from the dictionary
*
* @return array
*/
private function _getWordFromDictionary($pattern) {
// Content of the dictionary
$dictionary = file_get_contents('./dict');
// Match the available words in the dictionary based on pattern of the hint
preg_match_all("/^([$pattern]+)$/im", $dictionary, $possibleWords);
return array_shift($possibleWords); // All possible words from dictionary
}
}
/* @var $secret Secret */
$secret = new Secret('wild inovation suly');
echo $secret->getSecret();