password-analyzer/stat_struct.cpp

43 lines
1.2 KiB
C++
Raw Permalink Normal View History

2023-11-03 21:11:37 +08:00
#include <algorithm>
#include <map>
#include <sstream>
#include <vector>
#include "defs.hpp"
std::string get_struct(const std::string &password) {
auto mapper = [](char c) {
if (is_num(c))
return 'D';
else if (is_alpha(c))
return 'L';
else
return 'S';
};
auto v = password | std::views::transform(mapper);
std::ostringstream ret;
for (auto cur = v.begin(); cur != v.end();) {
auto ch = *cur;
auto nxt = std::adjacent_find(cur, v.end(), std::not_equal_to<>());
auto end = nxt == v.end();
ret << ch << std::distance(cur, nxt) + !end;
cur = end ? nxt : std::next(nxt);
}
return ret.str();
}
void stat_struct(const DataSource &source) {
2023-11-04 00:24:22 +08:00
timeit(fmt::format("stat_struct({})", magic_enum::enum_name(source)));
2023-11-03 21:11:37 +08:00
std::map<std::string, size_t> stat;
for (auto const &password : passwords(source)) ++stat[get_struct(password)];
std::vector<std::pair<std::string, size_t>> vec(stat.begin(), stat.end());
std::sort(vec.begin(), vec.end(), [](auto const &lhs, auto const &rhs) { return lhs.second > rhs.second; });
2023-11-05 16:01:45 +08:00
for (auto const &[struct_, count] : vec | std::views::take(15)) spdlog::info("{}: {}", struct_, count);
2023-11-03 21:11:37 +08:00
}