#include #include #include #include #include #include #include #include #include namespace { constexpr int length = 17; constexpr int word_count = 1 << length; constexpr int descendant_count = 1 << (length - 2); int erase_bit(int word, int position) { const int lower = (1 << position) - 1; return ((word >> 1) & ~lower) | (word & lower); } std::vector> make_descendants() { std::vector> descendants(word_count); for (int word = 0; word < word_count; ++word) { auto& values = descendants[word]; for (int first = 0; first < length - 1; ++first) { for (int second = first + 1; second < length; ++second) { values.push_back(erase_bit(erase_bit(word, second), first)); } } std::sort(values.begin(), values.end()); values.erase(std::unique(values.begin(), values.end()), values.end()); } return descendants; } bool disjoint(const std::vector& left, const std::vector& right) { auto a = left.begin(); auto b = right.begin(); while (a != left.end() && b != right.end()) { if (*a == *b) return false; if (*a < *b) ++a; else ++b; } return true; } class Search { public: Search(const std::vector>& descendants, std::mt19937& random) : descendants_(descendants), random_(random), owner_(descendant_count, -1), candidates_(word_count) { for (int word = 0; word < word_count; ++word) candidates_[word] = word; } void reset(const std::vector& code) { std::fill(owner_.begin(), owner_.end(), -1); code_.clear(); for (int word : code) add(word); } void perturb(int count) { for (int i = 0; i < count; ++i) { const int offset = random_() % code_.size(); remove(code_[offset]); } } void improve() { std::shuffle(candidates_.begin(), candidates_.end(), random_); for (int word : candidates_) { if (sole_blocker(word) == -1) add(word); } while (replace_one_with_two()) {} } const std::vector& code() const { return code_; } private: int sole_blocker(int word) const { int blocker = -1; for (int d : descendants_[word]) { const int owner = owner_[d]; if (owner < 0) continue; if (blocker >= 0 && owner != blocker) return -2; blocker = owner; } return blocker; } void add(int word) { for (int d : descendants_[word]) { if (owner_[d] >= 0) throw std::logic_error("overlapping descendants"); owner_[d] = word; } code_.push_back(word); } void remove(int word) { for (int d : descendants_[word]) owner_[d] = -1; auto position = std::find(code_.begin(), code_.end(), word); *position = code_.back(); code_.pop_back(); } bool replace_one_with_two() { std::vector> blocked_by(word_count); for (int word : candidates_) { const int blocker = sole_blocker(word); if (blocker == word || blocker == -2) continue; if (blocker == -1) { add(word); return true; } auto& options = blocked_by[blocker]; for (int other : options) { if (!disjoint(descendants_[word], descendants_[other])) continue; remove(blocker); add(word); add(other); return true; } options.push_back(word); } return false; } const std::vector>& descendants_; std::mt19937& random_; std::vector owner_; std::vector candidates_; std::vector code_; }; std::vector read_baseline(const char* filename) { std::ifstream input(filename); if (!input) throw std::runtime_error("cannot read starting code"); std::vector baseline; std::string word; while (input >> word) { if (word.size() != length || word.find_first_not_of("01") != std::string::npos) { throw std::runtime_error("invalid word in starting code"); } baseline.push_back(std::stoi(word, nullptr, 2)); } if (baseline.empty()) throw std::runtime_error("starting code is empty"); return baseline; } void write_code(const char* filename, std::vector code) { std::sort(code.begin(), code.end()); std::ofstream output(filename); output.exceptions(std::ios::failbit | std::ios::badbit); output << "{\n \"n\": " << length << ",\n \"words\": [\n"; for (std::size_t i = 0; i < code.size(); ++i) { output << " \"" << std::bitset(code[i]) << '"' << (i + 1 == code.size() ? "\n" : ",\n"); } output << " ]\n}\n"; } } // namespace int main(int argc, char** argv) { try { if (argc != 5) throw std::runtime_error("usage: search START_WORDS OUT_JSON SEED ROUNDS"); const auto baseline = read_baseline(argv[1]); const auto descendants = make_descendants(); std::mt19937 random(std::stoul(argv[3])); const int rounds = std::stoi(argv[4]); if (rounds < 1) throw std::runtime_error("rounds must be positive"); Search search(descendants, random); search.reset(baseline); auto incumbent = baseline; auto best = baseline; for (int round = 0; round < rounds; ++round) { search.reset(round % 100 == 0 ? best : incumbent); if (round != 0) search.perturb(1 + random() % 8); search.improve(); if (search.code().size() > best.size() || round == 0) { best = search.code(); write_code(argv[2], best); std::cout << "metric: code_size " << best.size() << '\n'; } if (search.code().size() >= incumbent.size() || (round % 17 == 0 && search.code().size() + 2 >= best.size())) { incumbent = search.code(); } if ((round + 1) % 25 == 0 || round + 1 == rounds) { std::cout << "progress: " << round + 1 << '/' << rounds << " rounds, best " << best.size() << std::endl; } } } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } }