#include #include #include #include int bin_to_dec (std::string bin) { int dec = 0, pow = 1; for (std::size_t i=bin.size(); i > 0; i--) { dec += pow * (bin[i-1] == '0' ? 0 : 1); pow *= 2; } return dec; } std::vector get_freq (std::vector bits) { std::vector freq; for (std::size_t i=0; i filter (std::vector bits, int inv, int sel) { std::vector res; auto freq = get_freq(bits); for (auto b : bits) { int f = freq[sel] * inv; if (f == 0) f += inv; if ((f > 0 ? '0' : '1') == b[sel]) { res.push_back(b); } } return res; } int main (int argc, char *argv[]) { if (argc != 2) return 1; std::string raw; std::getline(std::ifstream(argv[1]), raw, '\0'); std::vector bits; while (1) { auto token = raw.find("\n"); if (token != std::string::npos) { bits.push_back(raw.substr(0, token)); raw = raw.substr(token+1); } else { if (raw.size() > 0) bits.push_back(raw); break; } } auto freq = get_freq(bits); std::string gamma = "", epsilon = ""; for (std::size_t i=0; i 0 ? "1" : "0"; epsilon += freq[i] > 0 ? "0" : "1"; } int g = bin_to_dec(gamma); int e = bin_to_dec(epsilon); std::cout << "power level " << e*g << ", " << "epsilon " << e << " (" << epsilon << "), " << "gamma " << g << " (" << gamma << ")" << std::endl; std::vector o2 = bits; for (std::size_t i=0; i co2 = bits; for (std::size_t i=0; i