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
|
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <ostream>
#include <random>
#include <stdexcept>
#include <thread>
#include <utility>
#include <vector>
namespace {
const std::vector<char> availableElements {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'!', '@', '#', '$', '%', '^', '&', '*', '?', '<', '>', '~'
};
const std::vector<char>::size_type defaultCombinationSize = 8;
std::vector<char> elusiveCombination;
} // namespace
namespace src {
std::vector<char> createRandomCombination(std::vector<char>::size_type vcs = ::defaultCombinationSize)
{
std::default_random_engine dre(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<std::vector<char>::size_type> uid(0, ::availableElements.size() - 1);
auto getRandomIndex = std::bind(uid, dre);
std::vector<char> vc(vcs);
std::generate(vc.begin(), vc.end(), [&getRandomIndex]() -> char {
return ::availableElements.at(getRandomIndex());
});
return vc;
}
std::ostream & operator << (std::ostream &os, const std::vector<char> &vc)
{
for (char c: vc)
os << c;
return os;
}
std::pair<std::vector<char>, bool> first(std::vector<char> vc)
{
if (vc.size() == ::defaultCombinationSize)
return std::make_pair(std::vector<char>{}, false);
vc.push_back(::availableElements.front());
return std::make_pair(vc, true);
}
std::pair<std::vector<char>, bool> next(std::vector<char> vc)
{
if (vc.back() == ::availableElements.back())
return std::make_pair(std::vector<char>{}, false);
vc.back() = *++std::find(::availableElements.begin(), ::availableElements.end(), vc.back());
return std::make_pair(vc, true);
}
void whinyChecker(const std::vector<char> &candidateCombination, std::ostream &os = std::cout)
{
if (candidateCombination.size() != ::elusiveCombination.size())
throw std::length_error("candidateCombination and ::elusiveCombination are not the same size.");
else
if (std::equal(candidateCombination.begin(), candidateCombination.end(), ::elusiveCombination.begin()))
{
os << ::elusiveCombination << " == " << candidateCombination << std::endl;
throw std::logic_error("Solution was found, so why go on?");
}
else
os << ::elusiveCombination << " != " << candidateCombination << std::endl;
}
void whinyBacktrack(const std::vector<char> &target, const std::vector<char> &built)
{
if (target.size() == built.size())
{
whinyChecker(built);
return;
}
auto possibleSolution = first(built);
while (possibleSolution.second)
{
whinyBacktrack(target, possibleSolution.first);
possibleSolution = next(possibleSolution.first);
}
}
std::list<std::vector<char>> generateLimits()
{
std::list<std::vector<char>> lvc;
for (char c: ::availableElements)
{
std::vector<char> vc;
vc.push_back(c);
while (vc.size() < ::defaultCombinationSize)
vc.push_back(::availableElements.back());
lvc.push_back(vc);
}
return lvc;
}
} // namespace src
using src::operator<<;
#ifndef GCC_ACTUALLY_IMPLEMENTS_THREADS
int main()
{
::elusiveCombination = src::createRandomCombination();
src::whinyBacktrack(src::generateLimits().back(), {});
}
#else
int main()
{
::elusiveCombination = src::createRandomCombination();
std::list<std::thread> lt;
for (const std::vector<char> &vc: src::generateLimits())
lt.push_back(std::thread(src::whinyBacktrack, std::cref(vc), {}));
for (std::thread &t: lt)
t.join();
}
#endif
|