Created
April 4, 2015 02:01
-
-
Save kotarou3/a3bb9ac9c372033a01a2 to your computer and use it in GitHub Desktop.
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
#include <chrono> | |
#include <map> | |
#include <vector> | |
#include <string> | |
#include <cstdlib> | |
#include <cstdio> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
constexpr size_t passLength = 19; | |
constexpr size_t samples = 10; | |
const std::string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; | |
double tryPassword(const char* password) { | |
char* args[4] = { | |
"/levels/level12", | |
const_cast<char*>(password), | |
"aaaa", | |
nullptr | |
}; | |
char* env[1] = {nullptr}; | |
double mean = 0; | |
for (size_t tries = 0; tries < samples; ++tries) { | |
auto start = std::chrono::high_resolution_clock::now(); | |
if (vfork() == 0) { | |
execve(args[0], args, env); | |
std::abort(); | |
} | |
wait(nullptr); | |
auto end = std::chrono::high_resolution_clock::now(); | |
double timeTaken = (end - start).count(); | |
mean += (timeTaken - mean) / (double)samples; | |
} | |
return mean; | |
} | |
std::vector<std::string> getPermutations() { | |
std::vector<std::string> result; | |
result.reserve(letters.size() * letters.size() * letters.size()); | |
for (char a : letters) | |
for (char b : letters) | |
result.push_back({a, b}); | |
return result; | |
} | |
int main() { | |
std::vector<std::string> permutations = getPermutations(); | |
for (size_t startPosition = 17; startPosition > 1; startPosition -= 2) { | |
std::fprintf(stderr, "Doing position #%zu: ", startPosition); | |
char firstLetter = 0; | |
std::map<double, std::string> results; | |
std::string password(passLength, letters[0]); | |
for (const auto& permute : getPermutations()) { | |
if (permute[0] != firstLetter) { | |
std::fprintf(stderr, "%c", permute[0]); | |
firstLetter = permute[0]; | |
} | |
password.replace(startPosition, permute.size(), permute); | |
results[tryPassword(password.c_str())] = permute; | |
} | |
std::fputs("\n", stderr); | |
std::printf("Results at #%zu:", startPosition); | |
std::map<size_t, std::map<char, size_t>> counts; | |
size_t n = 0; | |
for (const auto& result : results) { | |
if (n > letters.size()) | |
break; | |
if (n < 6) | |
std::printf(" (%f: %s)", result.first, result.second.c_str()); | |
for (size_t c = 0; c < result.second.size(); ++c) | |
++counts[c][result.second[c]]; | |
++n; | |
} | |
std::string optimalPermute = ""; | |
for (const auto& count : counts) { | |
char result; | |
size_t resultCount = 0; | |
for (const auto& pair : count.second) | |
if (pair.second > resultCount) { | |
result = pair.first; | |
resultCount = pair.second; | |
} | |
optimalPermute += result; | |
} | |
std::printf("\nLikely: %s\n", optimalPermute.c_str()); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment