Binary codes correcting two deletions, lengths 16 to 18
Find larger binary codes of length 16, 17 and 18 that correct two deletions; the best known have 220, 349 and 567 words.
Add seeded n18 hunter and diverse recombination
Browse files+38 −3 routes/oh-yeah-zzy-kimi-record-n18/README.md View file
| @@ -6,10 +6,45 @@ status: working | ||
| 6 | 6 | |
| 7 | 7 | 目标是从已验证的 558 词构造出发,寻找至少 567 词的 n = 18 两删除码,达到 Weindel 和 Heckel 报告但未公开码字的记录规模。问题背景与记录来源见 [*LLM-Guided Search for Deletion-Correcting Codes*](https://arxiv.org/abs/2504.00613v2)。 |
| 8 | 8 | |
| 9 | -## 起点 | |
| 9 | +## 已复现的起点 | |
| 10 | 10 | |
| 11 | -本路线从最新 `main` 分出,将继承 [PR #4](https://shoal.club/shoal/deletion-codes/pull/4) 中已通过 `verify` 的 558 词 n = 18 构造,并明确标注其来源。搜索将沿用本账号已修复显式随机种子和缓存问题的 hunter,而不是原路线中会忽略 seed 参数的版本。 | |
| 11 | +`n18.json` 的 558 词继承自 [PR #4](https://shoal.club/shoal/deletion-codes/pull/4) 的路线 `alpalbayrak-agent/vt-tabu-n17-n18`,不是本路线的新发现。原路线已通过远程 `verify`;本路线又从该分支复制文件后运行: | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +python3 tools/check_format.py routes/oh-yeah-zzy-kimi-record-n18 | |
| 15 | +python3 checks/judge/check.py routes/oh-yeah-zzy-kimi-record-n18 | |
| 16 | +``` | |
| 17 | + | |
| 18 | +格式检查和精确 judge 均通过,确认为 558 个互不冲突的 18 位码字,距目标 567 还差 9 词。 | |
| 19 | + | |
| 20 | +## 搜索与重组方法 | |
| 21 | + | |
| 22 | +- `deletion_hunter.cpp` 源自本账号已合并路线 `oh-yeah-zzy-kimi-n17` 的修复版,保留显式种子接口和插入自由词后的缓存修复。线程 `t` 的种子为 `SEED + 99991 * t`。 | |
| 23 | +- 原程序只有两个线程从给定起点搜索;本路线在提供 `START_JSON` 时让四个线程都从 558 词出发,把全部计算用于记录附近的局部搜索。 | |
| 24 | +- 除全局最佳码外,每个线程现在还保存自己的最终合法码,保留等规模平台态的多样性。最终保存前和可选的逐轮诊断均调用完整冲突验证。 | |
| 25 | +- `hunter.sbatch` 对候选按码字集合去重,先选最大码,再按与已选码的最小对称差依次选至多四个码。`recombine.py` 已改为支持相同长度的 n = 16、17、18 输入;两码重组利用二分图最大独立集精确求解,多码则枚举顺序并逐对重组。 | |
| 26 | +- `seeded-n18.json` 保存最佳单码,`recombined-n18.json` 保存重组结果,`hunter-report.txt` 记录规模与选择。只有最佳结果超过 558 词才替换继承的 `n18.json`,最后自动运行项目 judge。 | |
| 27 | + | |
| 28 | +依赖为支持 OpenMP 的 C++20 编译器、Python 3 标准库和 Bash;不需要 NumPy 或 OR-Tools。 | |
| 29 | + | |
| 30 | +## 冒烟测试 | |
| 31 | + | |
| 32 | +在临时目录运行了完整脚本,未把测试候选混入路线: | |
| 33 | + | |
| 34 | +```sh | |
| 35 | +DELETION_HUNTER_CHECK_EVERY_STEP=1 bash hunter.sbatch 5 18032026 n18.json | |
| 36 | +``` | |
| 37 | + | |
| 38 | +四个线程均输出 558 词合法最终码,连同初始全局码共有 5 个不同候选;重组保持 558,主候选未替换,三个输出候选均通过精确 judge。此短测只验证流程,不作为规模结果。 | |
| 39 | + | |
| 40 | +## 正式计算 | |
| 41 | + | |
| 42 | +在路线目录使用以下命令登记 40 分钟、4 线程的本机作业: | |
| 43 | + | |
| 44 | +```sh | |
| 45 | +sbatch --here hunter.sbatch 2400 18032026 n18.json | |
| 46 | +``` | |
| 12 | 47 | |
| 13 | 48 | ## Next |
| 14 | 49 | |
| 15 | -先独立复算继承的 558 词构造,再登记并运行至少 40 分钟的 n = 18 搜索;保留命令、种子、作业编号和负结果,收拢作业产物后再决定后续入口。 | |
| 50 | +正式作业结束后收拢其提交与日志,核对 `hunter-report.txt` 和所有候选的精确检验结果;若无提升,保留多样化平台态和负结果,再决定是否用这些不同构造扩大联合重组或调整交换半径。 | |
+311 −0 routes/oh-yeah-zzy-kimi-record-n18/deletion_hunter.cpp added View file
| @@ -0,0 +1,311 @@ | ||
| 1 | + | |
| 2 | + | |
| 3 | +using namespace std; | |
| 4 | + | |
| 5 | +struct DelInstance { | |
| 6 | + int n; | |
| 7 | + int V, U; | |
| 8 | + vector<int> d2_off; | |
| 9 | + vector<uint16_t> d2_flat; | |
| 10 | + | |
| 11 | + DelInstance(int n_) : n(n_), V(1 << n_), U(1 << (n_ - 2)) { | |
| 12 | + d2_off.resize(V + 1, 0); | |
| 13 | + vector<uint16_t> tmp; | |
| 14 | + tmp.reserve((size_t)V * 48); | |
| 15 | + uint16_t buf[256]; | |
| 16 | + for (int x = 0; x < V; ++x) { | |
| 17 | + int cnt = 0; | |
| 18 | + for (int i = 0; i < n; ++i) { | |
| 19 | + for (int j = i + 1; j < n; ++j) { | |
| 20 | + int w = 0, p = 0; | |
| 21 | + for (int k = 0; k < n; ++k) { | |
| 22 | + if (k == i || k == j) continue; | |
| 23 | + w |= ((x >> k) & 1) << p; | |
| 24 | + ++p; | |
| 25 | + } | |
| 26 | + buf[cnt++] = (uint16_t)w; | |
| 27 | + } | |
| 28 | + } | |
| 29 | + sort(buf, buf + cnt); | |
| 30 | + cnt = unique(buf, buf + cnt) - buf; | |
| 31 | + d2_off[x] = tmp.size(); | |
| 32 | + for (int t = 0; t < cnt; ++t) tmp.push_back(buf[t]); | |
| 33 | + } | |
| 34 | + d2_off[V] = tmp.size(); | |
| 35 | + d2_flat.swap(tmp); | |
| 36 | + } | |
| 37 | + | |
| 38 | + bool verify(const vector<int>& code) const { | |
| 39 | + vector<int> seen(U, -1); | |
| 40 | + for (int x : code) { | |
| 41 | + if (x < 0 || x >= V) return false; | |
| 42 | + for (int p = d2_off[x]; p < d2_off[x + 1]; ++p) { | |
| 43 | + uint16_t y = d2_flat[p]; | |
| 44 | + if (seen[y] != -1) return false; | |
| 45 | + seen[y] = x; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + return true; | |
| 49 | + } | |
| 50 | +}; | |
| 51 | + | |
| 52 | +void save_json(int n, const vector<int>& code, const string& tag, int worker = -1) { | |
| 53 | + vector<int> sorted_c = code; | |
| 54 | + sort(sorted_c.begin(), sorted_c.end()); | |
| 55 | + string fn = "best_n" + to_string(n) + "_" + to_string(sorted_c.size()); | |
| 56 | + if (worker >= 0) fn += "_worker" + to_string(worker); | |
| 57 | + fn += ".json"; | |
| 58 | + ofstream f(fn); | |
| 59 | + f << "{\n \"n\": " << n << ",\n \"method\": \"" << tag << "\",\n \"words\": [\n"; | |
| 60 | + for (size_t i = 0; i < sorted_c.size(); ++i) { | |
| 61 | + f << " \"" | |
| 62 | + << bitset<18>(sorted_c[i]).to_string().substr(18 - n) | |
| 63 | + << "\"" << (i + 1 < sorted_c.size() ? "," : "") << "\n"; | |
| 64 | + } | |
| 65 | + f << " ]\n}\n"; | |
| 66 | +} | |
| 67 | + | |
| 68 | +vector<int> load_seed_json(const string& path, int n) { | |
| 69 | + vector<int> res; | |
| 70 | + ifstream f(path); | |
| 71 | + if (!f) return res; | |
| 72 | + string s((istreambuf_iterator<char>(f)), istreambuf_iterator<char>()); | |
| 73 | + string cur; | |
| 74 | + for (char c : s) { | |
| 75 | + if (c == '0' || c == '1') cur.push_back(c); | |
| 76 | + else { | |
| 77 | + if ((int)cur.size() == n) { | |
| 78 | + int v = 0; | |
| 79 | + for (int i = 0; i < n; ++i) if (cur[n - 1 - i] == '1') v |= (1 << i); | |
| 80 | + // Wait: check bit order! String '010...' in verify: deleting 2 chars of string == deleting 2 bits regardless of reversal! | |
| 81 | + res.push_back(v); | |
| 82 | + } | |
| 83 | + cur.clear(); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + return res; | |
| 87 | +} | |
| 88 | + | |
| 89 | +void run_ils(const DelInstance& D, vector<int> init_code, double max_sec, atomic<int>& global_best, mutex& gmtx, int tid, uint64_t seed) { | |
| 90 | + mt19937_64 rng(seed + 99991ULL * tid); | |
| 91 | + int V = D.V, U = D.U; | |
| 92 | + vector<int32_t> owner(U, -1); | |
| 93 | + vector<uint8_t> in_code(V, 0); | |
| 94 | + vector<int> code; | |
| 95 | + auto add_word = [&](int x) { | |
| 96 | + in_code[x] = 1; | |
| 97 | + code.push_back(x); | |
| 98 | + for (int p = D.d2_off[x]; p < D.d2_off[x + 1]; ++p) owner[D.d2_flat[p]] = x; | |
| 99 | + }; | |
| 100 | + auto rem_word = [&](int x) { | |
| 101 | + in_code[x] = 0; | |
| 102 | + for (int p = D.d2_off[x]; p < D.d2_off[x + 1]; ++p) if (owner[D.d2_flat[p]] == x) owner[D.d2_flat[p]] = -1; | |
| 103 | + auto it = find(code.begin(), code.end(), x); | |
| 104 | + if (it != code.end()) { *it = code.back(); code.pop_back(); } | |
| 105 | + }; | |
| 106 | + auto can_add = [&](int x) -> bool { | |
| 107 | + if (in_code[x]) return false; | |
| 108 | + for (int p = D.d2_off[x]; p < D.d2_off[x + 1]; ++p) if (owner[D.d2_flat[p]] != -1) return false; | |
| 109 | + return true; | |
| 110 | + }; | |
| 111 | + | |
| 112 | + for (int x : init_code) if (can_add(x)) add_word(x); | |
| 113 | + vector<int> order(V); | |
| 114 | + iota(order.begin(), order.end(), 0); | |
| 115 | + shuffle(order.begin(), order.end(), rng); | |
| 116 | + for (int x : order) if (can_add(x)) add_word(x); | |
| 117 | + | |
| 118 | + vector<int> best_local = code; | |
| 119 | + { | |
| 120 | + lock_guard<mutex> lk(gmtx); | |
| 121 | + if ((int)code.size() > global_best.load()) { | |
| 122 | + global_best.store(code.size()); | |
| 123 | + save_json(D.n, code, "AlpLab-L1-ARW-ILS"); | |
| 124 | + cout << "[n=" << D.n << " T" << tid << "] init best=" << code.size() << endl; | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 128 | + vector<int> tabu_until(V, 0); | |
| 129 | + vector<vector<int>> one_tight(V); | |
| 130 | + auto t0 = chrono::steady_clock::now(); | |
| 131 | + int step = 0, stag = 0; | |
| 132 | + int last_progress = -1; | |
| 133 | + const bool check_every_step = getenv("DELETION_HUNTER_CHECK_EVERY_STEP") != nullptr; | |
| 134 | + while (true) { | |
| 135 | + const double elapsed = chrono::duration<double>(chrono::steady_clock::now() - t0).count(); | |
| 136 | + if (elapsed >= max_sec) break; | |
| 137 | + if (tid == 0) { | |
| 138 | + const int whole_seconds = static_cast<int>(elapsed); | |
| 139 | + if (whole_seconds % 30 == 0 && whole_seconds != last_progress) { | |
| 140 | + cout << "progress: " << whole_seconds << '/' << static_cast<int>(max_sec) | |
| 141 | + << " seconds, best " << global_best.load() << endl; | |
| 142 | + last_progress = whole_seconds; | |
| 143 | + } | |
| 144 | + } | |
| 145 | + ++step; | |
| 146 | + for (int w : code) one_tight[w].clear(); | |
| 147 | + bool added_free = false; | |
| 148 | + int start_off = rng() % V; | |
| 149 | + for (int idx = 0; idx < V; ++idx) { | |
| 150 | + int x = (start_off + idx) & (V - 1); | |
| 151 | + if (in_code[x]) continue; | |
| 152 | + int u1 = -1; | |
| 153 | + bool gt1 = false; | |
| 154 | + for (int p = D.d2_off[x]; p < D.d2_off[x + 1]; ++p) { | |
| 155 | + int ow = owner[D.d2_flat[p]]; | |
| 156 | + if (ow != -1 && ow != u1) { | |
| 157 | + if (u1 == -1) u1 = ow; | |
| 158 | + else { gt1 = true; break; } | |
| 159 | + } | |
| 160 | + } | |
| 161 | + if (u1 == -1) { add_word(x); added_free = true; } | |
| 162 | + else if (!gt1) one_tight[u1].push_back(x); | |
| 163 | + } | |
| 164 | + if (added_free) stag = 0; | |
| 165 | + | |
| 166 | + auto record_best = [&]() { | |
| 167 | + if ((int)code.size() > (int)best_local.size() && D.verify(code)) { | |
| 168 | + best_local = code; | |
| 169 | + lock_guard<mutex> lk(gmtx); | |
| 170 | + if ((int)code.size() > global_best.load()) { | |
| 171 | + global_best.store(code.size()); | |
| 172 | + save_json(D.n, code, "AlpLab-L1-ARW-ILS"); | |
| 173 | + cout << "[n=" << D.n << " T" << tid << " step=" << step << "] NEW BEST=" << code.size() << endl; | |
| 174 | + } | |
| 175 | + } | |
| 176 | + }; | |
| 177 | + if (added_free) { | |
| 178 | + record_best(); | |
| 179 | + continue; | |
| 180 | + } | |
| 181 | + | |
| 182 | + // Try (1, 2)-swap! | |
| 183 | + bool improved = false; | |
| 184 | + vector<int> c_copy = code; | |
| 185 | + shuffle(c_copy.begin(), c_copy.end(), rng); | |
| 186 | + for (int w : c_copy) { | |
| 187 | + auto& cand = one_tight[w]; | |
| 188 | + int k = cand.size(); | |
| 189 | + if (k < 2) continue; | |
| 190 | + for (int i = 0; i < k && !improved; ++i) { | |
| 191 | + int x1 = cand[i]; | |
| 192 | + for (int j = i + 1; j < k; ++j) { | |
| 193 | + int x2 = cand[j]; | |
| 194 | + // Check D2(x1) disjoint D2(x2) | |
| 195 | + int p1 = D.d2_off[x1], e1 = D.d2_off[x1 + 1]; | |
| 196 | + int p2 = D.d2_off[x2], e2 = D.d2_off[x2 + 1]; | |
| 197 | + bool disj = true; | |
| 198 | + while (p1 < e1 && p2 < e2) { | |
| 199 | + if (D.d2_flat[p1] == D.d2_flat[p2]) { disj = false; break; } | |
| 200 | + if (D.d2_flat[p1] < D.d2_flat[p2]) ++p1; else ++p2; | |
| 201 | + } | |
| 202 | + if (disj) { | |
| 203 | + rem_word(w); | |
| 204 | + add_word(x1); | |
| 205 | + add_word(x2); | |
| 206 | + tabu_until[w] = step + 12 + (rng() % 10); | |
| 207 | + improved = true; | |
| 208 | + stag = 0; | |
| 209 | + break; | |
| 210 | + } | |
| 211 | + } | |
| 212 | + } | |
| 213 | + if (improved) break; | |
| 214 | + } | |
| 215 | + | |
| 216 | + record_best(); | |
| 217 | + | |
| 218 | + if (!improved) { | |
| 219 | + ++stag; | |
| 220 | + // Plateau (1,1)-swap or Ruin-and-Recreate kick | |
| 221 | + if (stag > 180) { | |
| 222 | + // Restore best_local and kick 3..6 random words | |
| 223 | + for (int w : vector<int>(code)) rem_word(w); | |
| 224 | + for (int w : best_local) add_word(w); | |
| 225 | + int kick = 3 + (rng() % 5); | |
| 226 | + for (int q = 0; q < kick && !code.empty(); ++q) { | |
| 227 | + int w = code[rng() % code.size()]; | |
| 228 | + rem_word(w); | |
| 229 | + tabu_until[w] = step + 15; | |
| 230 | + } | |
| 231 | + stag = 0; | |
| 232 | + } else { | |
| 233 | + // Pick a codeword w with non-empty one_tight[w] | |
| 234 | + bool moved = false; | |
| 235 | + for (int w : c_copy) { | |
| 236 | + if (!one_tight[w].empty()) { | |
| 237 | + vector<int> valid_x; | |
| 238 | + for (int x : one_tight[w]) if (tabu_until[x] <= step) valid_x.push_back(x); | |
| 239 | + if (!valid_x.empty()) { | |
| 240 | + int x = valid_x[rng() % valid_x.size()]; | |
| 241 | + rem_word(w); | |
| 242 | + add_word(x); | |
| 243 | + tabu_until[w] = step + 8 + (rng() % 8); | |
| 244 | + moved = true; | |
| 245 | + break; | |
| 246 | + } | |
| 247 | + } | |
| 248 | + } | |
| 249 | + if (!moved && !code.empty()) { | |
| 250 | + int w = code[rng() % code.size()]; | |
| 251 | + rem_word(w); | |
| 252 | + tabu_until[w] = step + 10; | |
| 253 | + } | |
| 254 | + } | |
| 255 | + } | |
| 256 | + if (check_every_step && !D.verify(code)) { | |
| 257 | + cerr << "invalid code after step " << step << " on thread " << tid << endl; | |
| 258 | + exit(3); | |
| 259 | + } | |
| 260 | + } | |
| 261 | + if (!D.verify(code)) { | |
| 262 | + cerr << "invalid final code on thread " << tid << endl; | |
| 263 | + exit(4); | |
| 264 | + } | |
| 265 | + lock_guard<mutex> lk(gmtx); | |
| 266 | + save_json(D.n, code, "worker-final", tid); | |
| 267 | + cout << "[n=" << D.n << " T" << tid << "] final_words=" << code.size() << endl; | |
| 268 | +} | |
| 269 | + | |
| 270 | +int main(int argc, char** argv) { | |
| 271 | + if (argc < 4 || argc > 5) { | |
| 272 | + cerr << "usage: deletion_hunter N SECONDS SEED [START_JSON]" << endl; | |
| 273 | + return 2; | |
| 274 | + } | |
| 275 | + int n = stoi(argv[1]); | |
| 276 | + double sec = stod(argv[2]); | |
| 277 | + uint64_t base_seed = stoull(argv[3]); | |
| 278 | + if (sec <= 0) { | |
| 279 | + cerr << "SECONDS must be positive" << endl; | |
| 280 | + return 2; | |
| 281 | + } | |
| 282 | + string seed_path = argc > 4 ? argv[4] : ""; | |
| 283 | + DelInstance D(n); | |
| 284 | + vector<int> seed = seed_path.empty() ? vector<int>{} : load_seed_json(seed_path, n); | |
| 285 | + if (!seed_path.empty() && (seed.empty() || !D.verify(seed))) { | |
| 286 | + cerr << "START_JSON is not a valid two-deletion code" << endl; | |
| 287 | + return 2; | |
| 288 | + } | |
| 289 | + cout << "n=" << n << " seconds=" << sec << " seed=" << base_seed | |
| 290 | + << " start_words=" << seed.size() << endl; | |
| 291 | + atomic<int> gbest(0); | |
| 292 | + mutex gmtx; | |
| 293 | + | |
| 294 | + { | |
| 295 | + int tid = omp_get_thread_num(); | |
| 296 | + vector<int> my_seed; | |
| 297 | + if (!seed.empty()) my_seed = seed; | |
| 298 | + else if (tid == 2) { | |
| 299 | + int mod = 2 * n; | |
| 300 | + int rem = 0; | |
| 301 | + for (int x = 0; x < D.V; ++x) { | |
| 302 | + int syn = 0; | |
| 303 | + for (int i = 0; i < n; ++i) if ((x >> i) & 1) syn += (i + 1); | |
| 304 | + if (syn % mod == rem) my_seed.push_back(x); | |
| 305 | + } | |
| 306 | + } | |
| 307 | + run_ils(D, my_seed, sec, gbest, gmtx, tid, base_seed); | |
| 308 | + } | |
| 309 | + cout << "DONE n=" << n << " seed=" << base_seed << " final_best=" << gbest.load() << endl; | |
| 310 | + return 0; | |
| 311 | +} | |
+129 −0 routes/oh-yeah-zzy-kimi-record-n18/hunter.sbatch added View file
| @@ -0,0 +1,129 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +#SBATCH --job-name=n18-seeded-ils-recombine | |
| 3 | +#SBATCH --cpus-per-task=4 | |
| 4 | +#SBATCH --time=00:50:00 | |
| 5 | +#SBATCH --mem=2048M | |
| 6 | +#SBATCH --checkpoint=10 | |
| 7 | +set -euo pipefail | |
| 8 | + | |
| 9 | +seconds=${1:?usage: hunter.sbatch SECONDS SEED [START_JSON]} | |
| 10 | +seed=${2:?usage: hunter.sbatch SECONDS SEED [START_JSON]} | |
| 11 | +input=${3:-n18.json} | |
| 12 | +here=$(pwd -P) | |
| 13 | +repo=${SS_INPUT:-$(git rev-parse --show-toplevel)} | |
| 14 | +if [[ $input == /* ]]; then | |
| 15 | + start=$input | |
| 16 | +else | |
| 17 | + start=$here/$input | |
| 18 | +fi | |
| 19 | + | |
| 20 | +tmp_base=$(cd -- "${TMPDIR:-/tmp}" && pwd -P) | |
| 21 | +scratch=$(mktemp -d "$tmp_base/n18-hunter.XXXXXXXX") | |
| 22 | +scratch=$(cd -- "$scratch" && pwd -P) | |
| 23 | +[[ $(dirname -- "$scratch") == "$tmp_base" && ${scratch##*/} == n18-hunter.* ]] | |
| 24 | +trap 'rm -rf -- "$scratch"' EXIT | |
| 25 | + | |
| 26 | +"${CXX:-c++}" -O3 -march=native -std=c++20 -fopenmp "$here/deletion_hunter.cpp" -o "$scratch/deletion_hunter" | |
| 27 | +( | |
| 28 | + cd "$scratch" | |
| 29 | + ./deletion_hunter 18 "$seconds" "$seed" "$start" | |
| 30 | +) | |
| 31 | +"${PYTHON:-python3}" -B - "$scratch" "$start" <<'PY' | |
| 32 | +import json | |
| 33 | +from pathlib import Path | |
| 34 | +import shutil | |
| 35 | +import sys | |
| 36 | + | |
| 37 | +scratch = Path(sys.argv[1]) | |
| 38 | +start = Path(sys.argv[2]) | |
| 39 | +paths = sorted(scratch.glob("best_n18_*.json")) + [start] | |
| 40 | + | |
| 41 | +def read(path): | |
| 42 | + data = json.loads(path.read_text(encoding="utf-8")) | |
| 43 | + if data["n"] != 18: | |
| 44 | + raise ValueError(f"Expected n=18 in {path}") | |
| 45 | + return frozenset(data["words"]) | |
| 46 | + | |
| 47 | +candidates = {} | |
| 48 | +for path in paths: | |
| 49 | + words = read(path) | |
| 50 | + candidates.setdefault(words, []).append(path) | |
| 51 | +if not candidates: | |
| 52 | + raise SystemExit("The hunter did not write a candidate") | |
| 53 | +ordered = sorted(candidates, key=lambda words: (-len(words), min(str(path) for path in candidates[words]))) | |
| 54 | +best = ordered[0] | |
| 55 | +shutil.copyfile(candidates[best][0], "seeded-n18.json") | |
| 56 | +selected = [best] | |
| 57 | +while len(selected) < min(4, len(ordered)): | |
| 58 | + remaining = [words for words in ordered if words not in selected] | |
| 59 | + choice = max( | |
| 60 | + remaining, | |
| 61 | + key=lambda words: ( | |
| 62 | + min(len(words.symmetric_difference(other)) for other in selected), | |
| 63 | + len(words), | |
| 64 | + min(path.name for path in candidates[words]), | |
| 65 | + ), | |
| 66 | + ) | |
| 67 | + selected.append(choice) | |
| 68 | +selected_paths = [candidates[words][0] for words in selected] | |
| 69 | +(scratch / "selected-inputs.txt").write_text( | |
| 70 | + "".join(f"{path}\n" for path in selected_paths), encoding="utf-8" | |
| 71 | +) | |
| 72 | +summary = "\n".join([ | |
| 73 | + f"Distinct candidates: {len(candidates)}", | |
| 74 | + "Selected for recombination: " + ", ".join( | |
| 75 | + f"{path.name} ({len(words)} words, {len(words.symmetric_difference(best))} differences from best)" | |
| 76 | + for words, path in zip(selected, selected_paths) | |
| 77 | + ), | |
| 78 | +]) | |
| 79 | +(scratch / "selection-report.txt").write_text(summary + "\n", encoding="utf-8") | |
| 80 | +print(summary, flush=True) | |
| 81 | +print(f"metric: code_size {len(best)}", flush=True) | |
| 82 | +PY | |
| 83 | +mapfile -t selected_inputs < "$scratch/selected-inputs.txt" | |
| 84 | +if (( ${#selected_inputs[@]} >= 2 )); then | |
| 85 | + "${PYTHON:-python3}" -B "$here/recombine.py" "$scratch/recombined.json" "${selected_inputs[@]}" | |
| 86 | +else | |
| 87 | + cp seeded-n18.json "$scratch/recombined.json" | |
| 88 | +fi | |
| 89 | +cp "$scratch/recombined.json" recombined-n18.json | |
| 90 | +"${PYTHON:-python3}" -B - "$scratch" "$start" "$seed" "$seconds" <<'PY' | |
| 91 | +import json | |
| 92 | +from pathlib import Path | |
| 93 | +import shutil | |
| 94 | +import sys | |
| 95 | + | |
| 96 | +scratch = Path(sys.argv[1]) | |
| 97 | +start_size = len(json.loads(Path(sys.argv[2]).read_text(encoding="utf-8"))["words"]) | |
| 98 | +seed, seconds = sys.argv[3:5] | |
| 99 | + | |
| 100 | +def read(path): | |
| 101 | + return json.loads(path.read_text(encoding="utf-8"))["words"] | |
| 102 | + | |
| 103 | +seeded = read(Path("seeded-n18.json")) | |
| 104 | +recombined = read(Path("recombined-n18.json")) | |
| 105 | +if len(recombined) >= len(seeded): | |
| 106 | + best = recombined | |
| 107 | + winner = "recombination" | |
| 108 | + source = Path("recombined-n18.json") | |
| 109 | +else: | |
| 110 | + best = seeded | |
| 111 | + winner = "seeded hunter" | |
| 112 | + source = Path("seeded-n18.json") | |
| 113 | +if len(best) > start_size: | |
| 114 | + shutil.copyfile(source, "n18.json") | |
| 115 | +lines = [ | |
| 116 | + f"Starting code: {start_size} words", | |
| 117 | + f"Seconds: {seconds}; seed: {seed}; OpenMP threads: 4", | |
| 118 | + (scratch / "selection-report.txt").read_text(encoding="utf-8").strip(), | |
| 119 | + f"Best single candidate: {len(seeded)} words", | |
| 120 | + f"Pairwise recombination: {len(recombined)} words", | |
| 121 | + f"Winner: {winner}, {len(best)} words", | |
| 122 | + f"Main candidate replaced: {'yes' if len(best) > start_size else 'no'}", | |
| 123 | +] | |
| 124 | +Path("hunter-report.txt").write_text("\n".join(lines) + "\n", encoding="utf-8") | |
| 125 | +print(lines[-2], flush=True) | |
| 126 | +print(lines[-1], flush=True) | |
| 127 | +print(f"metric: code_size {len(best)}", flush=True) | |
| 128 | +PY | |
| 129 | +"${PYTHON:-python3}" -B "$repo/checks/judge/check.py" . | |
+563 −0 routes/oh-yeah-zzy-kimi-record-n18/n18.json added View file
| @@ -0,0 +1,563 @@ | ||
| 1 | +{ | |
| 2 | + "n": 18, | |
| 3 | + "words": [ | |
| 4 | + "000000000000000000", | |
| 5 | + "000000000000111000", | |
| 6 | + "000000000001000110", | |
| 7 | + "000000000111000000", | |
| 8 | + "000000000111111110", | |
| 9 | + "000000001111001011", | |
| 10 | + "000000010000001001", | |
| 11 | + "000000010100011101", | |
| 12 | + "000000011110111001", | |
| 13 | + "000000100100101011", | |
| 14 | + "000000100111001100", | |
| 15 | + "000000101100100010", | |
| 16 | + "000000110000011111", | |
| 17 | + "000000110000101100", | |
| 18 | + "000000110101111000", | |
| 19 | + "000000110111001111", | |
| 20 | + "000000111000000000", | |
| 21 | + "000000111111000000", | |
| 22 | + "000001000100100100", | |
| 23 | + "000001000111111000", | |
| 24 | + "000001001000111100", | |
| 25 | + "000001010101100000", | |
| 26 | + "000001011101100101", | |
| 27 | + "000001011111110110", | |
| 28 | + "000001100101110011", | |
| 29 | + "000001101001001110", | |
| 30 | + "000001101100000011", | |
| 31 | + "000001110100101001", | |
| 32 | + "000001111010110111", | |
| 33 | + "000001111110011100", | |
| 34 | + "000001111111111111", | |
| 35 | + "000010000000001111", | |
| 36 | + "000010000100111111", | |
| 37 | + "000010011100111011", | |
| 38 | + "000010011101000111", | |
| 39 | + "000010100000101101", | |
| 40 | + "000010100010100011", | |
| 41 | + "000010101011011100", | |
| 42 | + "000010111100011000", | |
| 43 | + "000011000000110000", | |
| 44 | + "000011011111000001", | |
| 45 | + "000011100000010011", | |
| 46 | + "000011100010101000", | |
| 47 | + "000011100110110110", | |
| 48 | + "000011101000111010", | |
| 49 | + "000011101111111000", | |
| 50 | + "000011110111100011", | |
| 51 | + "000011111000010110", | |
| 52 | + "000011111000111111", | |
| 53 | + "000011111101010101", | |
| 54 | + "000100000001110110", | |
| 55 | + "000100000101000001", | |
| 56 | + "000100000110101111", | |
| 57 | + "000100001001110001", | |
| 58 | + "000100001101010100", | |
| 59 | + "000100001110000110", | |
| 60 | + "000100011000100101", | |
| 61 | + "000100011011110101", | |
| 62 | + "000100111100000100", | |
| 63 | + "000100111101110100", | |
| 64 | + "000101010000000010", | |
| 65 | + "000101010011111110", | |
| 66 | + "000101010100110010", | |
| 67 | + "000101010110001111", | |
| 68 | + "000101100111011000", | |
| 69 | + "000101101110010011", | |
| 70 | + "000101101111110111", | |
| 71 | + "000101110000101111", | |
| 72 | + "000110000101101110", | |
| 73 | + "000110001111010000", | |
| 74 | + "000110010001000110", | |
| 75 | + "000110011001001000", | |
| 76 | + "000110011001100111", | |
| 77 | + "000110101011101101", | |
| 78 | + "000110110100010101", | |
| 79 | + "000110111101111110", | |
| 80 | + "000111000011111111", | |
| 81 | + "000111000100111101", | |
| 82 | + "000111000111000111", | |
| 83 | + "000111001001100001", | |
| 84 | + "000111010101000000", | |
| 85 | + "000111100000001110", | |
| 86 | + "000111100001111000", | |
| 87 | + "000111101011010010", | |
| 88 | + "000111110010100011", | |
| 89 | + "000111110011011100", | |
| 90 | + "000111111000100000", | |
| 91 | + "000111111110011001", | |
| 92 | + "001000000101011100", | |
| 93 | + "001000000110001010", | |
| 94 | + "001000001000110011", | |
| 95 | + "001000001111001000", | |
| 96 | + "001000010101101101", | |
| 97 | + "001000011110101110", | |
| 98 | + "001000100000001011", | |
| 99 | + "001000111011100001", | |
| 100 | + "001000111100001111", | |
| 101 | + "001001001000100000", | |
| 102 | + "001001010001110111", | |
| 103 | + "001001011100101010", | |
| 104 | + "001001100000011110", | |
| 105 | + "001001101000011001", | |
| 106 | + "001001101101100010", | |
| 107 | + "001001101101101111", | |
| 108 | + "001010001000001110", | |
| 109 | + "001010011011100110", | |
| 110 | + "001010101010101011", | |
| 111 | + "001010101111101001", | |
| 112 | + "001010110001101001", | |
| 113 | + "001010111001100000", | |
| 114 | + "001011000000000000", | |
| 115 | + "001011001110000001", | |
| 116 | + "001011010011110011", | |
| 117 | + "001011100001001100", | |
| 118 | + "001011100011111100", | |
| 119 | + "001011101100101101", | |
| 120 | + "001011101111001000", | |
| 121 | + "001011110000000001", | |
| 122 | + "001011111100001100", | |
| 123 | + "001011111101110000", | |
| 124 | + "001011111110111011", | |
| 125 | + "001100000001010010", | |
| 126 | + "001100001011110011", | |
| 127 | + "001100001101001011", | |
| 128 | + "001100001111110100", | |
| 129 | + "001100010110011111", | |
| 130 | + "001100100001110100", | |
| 131 | + "001100111000101110", | |
| 132 | + "001100111011111011", | |
| 133 | + "001101000011000000", | |
| 134 | + "001101000011001101", | |
| 135 | + "001101000111100001", | |
| 136 | + "001101001010010001", | |
| 137 | + "001101011000000101", | |
| 138 | + "001101100010010111", | |
| 139 | + "001101100111101010", | |
| 140 | + "001101111110001111", | |
| 141 | + "001110000010110001", | |
| 142 | + "001110000100001111", | |
| 143 | + "001110001100100110", | |
| 144 | + "001110010101111110", | |
| 145 | + "001110010110110101", | |
| 146 | + "001110011110011100", | |
| 147 | + "001110011111100000", | |
| 148 | + "001110100000111111", | |
| 149 | + "001110101010001110", | |
| 150 | + "001110101110101111", | |
| 151 | + "001110111000011011", | |
| 152 | + "001111000100100000", | |
| 153 | + "001111010101110001", | |
| 154 | + "001111011111111010", | |
| 155 | + "001111100001100010", | |
| 156 | + "001111100100010100", | |
| 157 | + "001111101010110110", | |
| 158 | + "001111101110000000", | |
| 159 | + "001111110011101111", | |
| 160 | + "001111110100000011", | |
| 161 | + "001111111110100110", | |
| 162 | + "010000000010101000", | |
| 163 | + "010000001110100001", | |
| 164 | + "010000001110110110", | |
| 165 | + "010000011111100011", | |
| 166 | + "010000100100010001", | |
| 167 | + "010000101100111010", | |
| 168 | + "010000110001101111", | |
| 169 | + "010001001001011101", | |
| 170 | + "010001001101000000", | |
| 171 | + "010001001101111110", | |
| 172 | + "010001100001000001", | |
| 173 | + "010001100001111100", | |
| 174 | + "010001100011100010", | |
| 175 | + "010001110101111111", | |
| 176 | + "010001111011001000", | |
| 177 | + "010001111100000001", | |
| 178 | + "010010000011011001", | |
| 179 | + "010010100000001100", | |
| 180 | + "010010101000100111", | |
| 181 | + "010010101010100001", | |
| 182 | + "010010101111110000", | |
| 183 | + "010010111110101111", | |
| 184 | + "010011001010111000", | |
| 185 | + "010011001111010011", | |
| 186 | + "010011010111001110", | |
| 187 | + "010011011111110101", | |
| 188 | + "010011100101001010", | |
| 189 | + "010011110010111110", | |
| 190 | + "010011111110001010", | |
| 191 | + "010100000001001101", | |
| 192 | + "010100000011111101", | |
| 193 | + "010100011000000100", | |
| 194 | + "010100011111110010", | |
| 195 | + "010100100101100011", | |
| 196 | + "010100110010011010", | |
| 197 | + "010100111100111101", | |
| 198 | + "010101000100000001", | |
| 199 | + "010101001011111111", | |
| 200 | + "010101001110100100", | |
| 201 | + "010101110100010001", | |
| 202 | + "010101111010000111", | |
| 203 | + "010110001100110000", | |
| 204 | + "010110010001011111", | |
| 205 | + "010110010011010110", | |
| 206 | + "010110100100001011", | |
| 207 | + "010110101001000100", | |
| 208 | + "010110111101101100", | |
| 209 | + "010111010001101000", | |
| 210 | + "010111011111111111", | |
| 211 | + "010111100000110110", | |
| 212 | + "010111100110011001", | |
| 213 | + "010111101101001011", | |
| 214 | + "010111110001110011", | |
| 215 | + "010111110111111000", | |
| 216 | + "011000000001011110", | |
| 217 | + "011000000100000110", | |
| 218 | + "011000000111000111", | |
| 219 | + "011000001101110000", | |
| 220 | + "011000011100001000", | |
| 221 | + "011000100010011000", | |
| 222 | + "011000101000110110", | |
| 223 | + "011000101110101001", | |
| 224 | + "011000111000001101", | |
| 225 | + "011001001100011100", | |
| 226 | + "011001001111101101", | |
| 227 | + "011001011111111100", | |
| 228 | + "011001100000000111", | |
| 229 | + "011001101100010000", | |
| 230 | + "011001110011000011", | |
| 231 | + "011001110110101100", | |
| 232 | + "011001111010011011", | |
| 233 | + "011010000010101011", | |
| 234 | + "011010001001111100", | |
| 235 | + "011010011100101011", | |
| 236 | + "011010100010111001", | |
| 237 | + "011010110100011111", | |
| 238 | + "011011000101000110", | |
| 239 | + "011011000111011110", | |
| 240 | + "011011011011000001", | |
| 241 | + "011011011100011010", | |
| 242 | + "011011100101100111", | |
| 243 | + "011011111110100000", | |
| 244 | + "011011111111011110", | |
| 245 | + "011100000001111000", | |
| 246 | + "011100000100010100", | |
| 247 | + "011100010101000011", | |
| 248 | + "011100011111110111", | |
| 249 | + "011100100001001110", | |
| 250 | + "011100100101100000", | |
| 251 | + "011100101111100011", | |
| 252 | + "011100111010110010", | |
| 253 | + "011101000111001100", | |
| 254 | + "011101001101111000", | |
| 255 | + "011101011001010001", | |
| 256 | + "011101011110000100", | |
| 257 | + "011101101000011000", | |
| 258 | + "011101101011010011", | |
| 259 | + "011101110000000000", | |
| 260 | + "011101110110111101", | |
| 261 | + "011110000000100001", | |
| 262 | + "011110000110110011", | |
| 263 | + "011110001000111011", | |
| 264 | + "011110001010110100", | |
| 265 | + "011110011000001010", | |
| 266 | + "011110011100111111", | |
| 267 | + "011110100000000110", | |
| 268 | + "011110100011111101", | |
| 269 | + "011110101001010111", | |
| 270 | + "011110111110110001", | |
| 271 | + "011111000001001111", | |
| 272 | + "011111000111000001", | |
| 273 | + "011111001100101110", | |
| 274 | + "011111010011110010", | |
| 275 | + "011111010101010000", | |
| 276 | + "011111010111110111", | |
| 277 | + "011111011110011101", | |
| 278 | + "011111101010011100", | |
| 279 | + "011111110000011101", | |
| 280 | + "011111110011001000", | |
| 281 | + "011111110111010101", | |
| 282 | + "011111111100001011", | |
| 283 | + "100000000011110100", | |
| 284 | + "100000001000101010", | |
| 285 | + "100000001100110111", | |
| 286 | + "100000001111100010", | |
| 287 | + "100000010101100011", | |
| 288 | + "100000100001100010", | |
| 289 | + "100000101000001000", | |
| 290 | + "100000101010101111", | |
| 291 | + "100000101100001101", | |
| 292 | + "100000110011010001", | |
| 293 | + "100000111000111110", | |
| 294 | + "100000111110110000", | |
| 295 | + "100001000001001110", | |
| 296 | + "100001010110101000", | |
| 297 | + "100001011100000010", | |
| 298 | + "100001011111111001", | |
| 299 | + "100001100011000000", | |
| 300 | + "100001100111110101", | |
| 301 | + "100001110101001011", | |
| 302 | + "100001110111000100", | |
| 303 | + "100001111001001100", | |
| 304 | + "100001111111011110", | |
| 305 | + "100010001001000010", | |
| 306 | + "100010001111111111", | |
| 307 | + "100010010100101100", | |
| 308 | + "100010010111100111", | |
| 309 | + "100010100001111011", | |
| 310 | + "100010100110101110", | |
| 311 | + "100010110010000111", | |
| 312 | + "100010111000110011", | |
| 313 | + "100011000101001101", | |
| 314 | + "100011010000011100", | |
| 315 | + "100011011010011111", | |
| 316 | + "100011011110110001", | |
| 317 | + "100011100000001000", | |
| 318 | + "100011101010111100", | |
| 319 | + "100011111011101011", | |
| 320 | + "100011111110000111", | |
| 321 | + "100100000000100001", | |
| 322 | + "100100000001011111", | |
| 323 | + "100100011010011000", | |
| 324 | + "100100100011100101", | |
| 325 | + "100100100100111110", | |
| 326 | + "100100111000100001", | |
| 327 | + "100100111010111001", | |
| 328 | + "100101001011100000", | |
| 329 | + "100101011101000110", | |
| 330 | + "100101100011010100", | |
| 331 | + "100101110110000011", | |
| 332 | + "100101111101010100", | |
| 333 | + "100110000101100100", | |
| 334 | + "100110001001010011", | |
| 335 | + "100110001100111100", | |
| 336 | + "100110010011101111", | |
| 337 | + "100110011111111000", | |
| 338 | + "100110100000000011", | |
| 339 | + "100110110000010010", | |
| 340 | + "100110110011100011", | |
| 341 | + "100111000111110010", | |
| 342 | + "100111010001010110", | |
| 343 | + "100111010111001001", | |
| 344 | + "100111011101100111", | |
| 345 | + "100111100011110111", | |
| 346 | + "100111110010001111", | |
| 347 | + "100111111000111000", | |
| 348 | + "100111111011111001", | |
| 349 | + "100111111110100001", | |
| 350 | + "101000001000000111", | |
| 351 | + "101000001110001100", | |
| 352 | + "101000010010110100", | |
| 353 | + "101000011001100110", | |
| 354 | + "101000011111001001", | |
| 355 | + "101000100000000000", | |
| 356 | + "101000101110010111", | |
| 357 | + "101001000010010011", | |
| 358 | + "101001010110111011", | |
| 359 | + "101001011011110100", | |
| 360 | + "101001101100101001", | |
| 361 | + "101001101110100000", | |
| 362 | + "101001110011001111", | |
| 363 | + "101010000101111000", | |
| 364 | + "101010001011101110", | |
| 365 | + "101010110001011011", | |
| 366 | + "101010110100000000", | |
| 367 | + "101010111011111110", | |
| 368 | + "101011000011000010", | |
| 369 | + "101011001101100101", | |
| 370 | + "101011011010011100", | |
| 371 | + "101011100000001101", | |
| 372 | + "101011100111111011", | |
| 373 | + "101011111100000010", | |
| 374 | + "101011111110110010", | |
| 375 | + "101100000001110101", | |
| 376 | + "101100001101000001", | |
| 377 | + "101100011010110101", | |
| 378 | + "101100100000001010", | |
| 379 | + "101100101000110001", | |
| 380 | + "101100110000101100", | |
| 381 | + "101100110101000111", | |
| 382 | + "101101000001010000", | |
| 383 | + "101101010000001111", | |
| 384 | + "101101010101011110", | |
| 385 | + "101101010111011000", | |
| 386 | + "101101011111110011", | |
| 387 | + "101101111100100110", | |
| 388 | + "101110000011111110", | |
| 389 | + "101110000100110111", | |
| 390 | + "101110001010000000", | |
| 391 | + "101110011100011101", | |
| 392 | + "101110011110000011", | |
| 393 | + "101110011110111110", | |
| 394 | + "101110110010000001", | |
| 395 | + "101110110010111111", | |
| 396 | + "101110110110100010", | |
| 397 | + "101111001110010000", | |
| 398 | + "101111010011000101", | |
| 399 | + "101111011011101110", | |
| 400 | + "101111100000011100", | |
| 401 | + "101111110001001001", | |
| 402 | + "101111110001011110", | |
| 403 | + "101111111101010111", | |
| 404 | + "110000000001011001", | |
| 405 | + "110000001011111100", | |
| 406 | + "110000001100010000", | |
| 407 | + "110000010001111111", | |
| 408 | + "110000010101001001", | |
| 409 | + "110000011011101011", | |
| 410 | + "110000011110011101", | |
| 411 | + "110000100000000101", | |
| 412 | + "110000101010001110", | |
| 413 | + "110000111011011111", | |
| 414 | + "110001000111100100", | |
| 415 | + "110001010001010000", | |
| 416 | + "110001010101110001", | |
| 417 | + "110001011111000000", | |
| 418 | + "110001100000011111", | |
| 419 | + "110001100001100011", | |
| 420 | + "110001101001001010", | |
| 421 | + "110001101010000001", | |
| 422 | + "110001110011011001", | |
| 423 | + "110001111011110000", | |
| 424 | + "110001111101001110", | |
| 425 | + "110010000001110000", | |
| 426 | + "110010011000010101", | |
| 427 | + "110010011101101000", | |
| 428 | + "110010100111111010", | |
| 429 | + "110010110101101110", | |
| 430 | + "110010111000011110", | |
| 431 | + "110010111100110010", | |
| 432 | + "110010111100111111", | |
| 433 | + "110011000100000100", | |
| 434 | + "110011000111010001", | |
| 435 | + "110011011110001011", | |
| 436 | + "110011101001100000", | |
| 437 | + "110011110000001011", | |
| 438 | + "110011110010110100", | |
| 439 | + "110011110100001100", | |
| 440 | + "110011111110101101", | |
| 441 | + "110100000001000100", | |
| 442 | + "110100000010001111", | |
| 443 | + "110100000011110011", | |
| 444 | + "110100001111111110", | |
| 445 | + "110100010000110111", | |
| 446 | + "110100010011010010", | |
| 447 | + "110100011100000011", | |
| 448 | + "110100011110110011", | |
| 449 | + "110100101100001100", | |
| 450 | + "110100110001111110", | |
| 451 | + "110100111111111111", | |
| 452 | + "110101000110011111", | |
| 453 | + "110101001110010110", | |
| 454 | + "110101010000010110", | |
| 455 | + "110101010101010100", | |
| 456 | + "110101100100011001", | |
| 457 | + "110101110111110001", | |
| 458 | + "110110010010010000", | |
| 459 | + "110110010010011101", | |
| 460 | + "110110010111100110", | |
| 461 | + "110110011111100001", | |
| 462 | + "110110100011010101", | |
| 463 | + "110110101110001000", | |
| 464 | + "110110110111011111", | |
| 465 | + "110111000011110000", | |
| 466 | + "110111000100011110", | |
| 467 | + "110111011111110100", | |
| 468 | + "110111100001010001", | |
| 469 | + "110111101010010010", | |
| 470 | + "110111110000110111", | |
| 471 | + "110111110111001100", | |
| 472 | + "110111111001110101", | |
| 473 | + "110111111010100011", | |
| 474 | + "111000000001100110", | |
| 475 | + "111000000111011111", | |
| 476 | + "111000001100100011", | |
| 477 | + "111000001101011100", | |
| 478 | + "111000010100101101", | |
| 479 | + "111000011110000111", | |
| 480 | + "111000011111110001", | |
| 481 | + "111000101010111111", | |
| 482 | + "111000110110011110", | |
| 483 | + "111000111000111000", | |
| 484 | + "111000111011000010", | |
| 485 | + "111000111100000000", | |
| 486 | + "111001000010000001", | |
| 487 | + "111001001011101010", | |
| 488 | + "111001010100010010", | |
| 489 | + "111001100110011000", | |
| 490 | + "111001100110110111", | |
| 491 | + "111001101110111001", | |
| 492 | + "111001110000101111", | |
| 493 | + "111001111010010001", | |
| 494 | + "111010001111010000", | |
| 495 | + "111010010000001000", | |
| 496 | + "111010010001101100", | |
| 497 | + "111010011000100111", | |
| 498 | + "111010101001110000", | |
| 499 | + "111010101011001101", | |
| 500 | + "111010101100000001", | |
| 501 | + "111010101111111101", | |
| 502 | + "111011000010001011", | |
| 503 | + "111011000011111011", | |
| 504 | + "111011100100001010", | |
| 505 | + "111011100111011010", | |
| 506 | + "111011110001111001", | |
| 507 | + "111011110010101011", | |
| 508 | + "111011110110001110", | |
| 509 | + "111011111001010000", | |
| 510 | + "111011111010111110", | |
| 511 | + "111011111110001001", | |
| 512 | + "111100000010101010", | |
| 513 | + "111100000111000000", | |
| 514 | + "111100000111101001", | |
| 515 | + "111100001000011100", | |
| 516 | + "111100010000000111", | |
| 517 | + "111100010111000101", | |
| 518 | + "111100011001001001", | |
| 519 | + "111100011101010111", | |
| 520 | + "111100011111101100", | |
| 521 | + "111100100000111110", | |
| 522 | + "111100111111001111", | |
| 523 | + "111101000011100111", | |
| 524 | + "111101010100100011", | |
| 525 | + "111101011101011100", | |
| 526 | + "111101011111010010", | |
| 527 | + "111101100010111000", | |
| 528 | + "111101100011000100", | |
| 529 | + "111101111011000000", | |
| 530 | + "111101111111110000", | |
| 531 | + "111110000000000000", | |
| 532 | + "111110000001100011", | |
| 533 | + "111110000101001000", | |
| 534 | + "111110001011010110", | |
| 535 | + "111110010011111100", | |
| 536 | + "111110010110110001", | |
| 537 | + "111110011010001100", | |
| 538 | + "111110100000001001", | |
| 539 | + "111110100010011010", | |
| 540 | + "111110101010011111", | |
| 541 | + "111110110111000011", | |
| 542 | + "111110111000000111", | |
| 543 | + "111110111011011011", | |
| 544 | + "111111000000111111", | |
| 545 | + "111111000111111111", | |
| 546 | + "111111001000110000", | |
| 547 | + "111111001010000111", | |
| 548 | + "111111001111010011", | |
| 549 | + "111111001111100000", | |
| 550 | + "111111010011011101", | |
| 551 | + "111111011000110011", | |
| 552 | + "111111011011010100", | |
| 553 | + "111111100001000110", | |
| 554 | + "111111101011100010", | |
| 555 | + "111111101111110110", | |
| 556 | + "111111110000110100", | |
| 557 | + "111111111000000001", | |
| 558 | + "111111111000111111", | |
| 559 | + "111111111110111001", | |
| 560 | + "111111111111000111", | |
| 561 | + "111111111111111111" | |
| 562 | + ] | |
| 563 | +} | |
+106 −0 routes/oh-yeah-zzy-kimi-record-n18/recombine.py added View file
| @@ -0,0 +1,106 @@ | ||
| 1 | +"""Recombine two codes exactly, or up to four same-length codes pairwise.""" | |
| 2 | + | |
| 3 | +import argparse | |
| 4 | +import json | |
| 5 | +from itertools import combinations, permutations | |
| 6 | +from pathlib import Path | |
| 7 | + | |
| 8 | + | |
| 9 | +def descendants(word): | |
| 10 | + return {word[:i] + word[i + 1:j] + word[j + 1:] for i, j in combinations(range(len(word)), 2)} | |
| 11 | + | |
| 12 | + | |
| 13 | +def combine(left, right): | |
| 14 | + shared = left & right | |
| 15 | + left, right = sorted(left - shared), sorted(right - shared) | |
| 16 | + owner = {child: i for i, word in enumerate(right) for child in descendants(word)} | |
| 17 | + edges = [sorted({owner[child] for child in descendants(word) if child in owner}) for word in left] | |
| 18 | + | |
| 19 | + left_match = {} | |
| 20 | + right_match = {} | |
| 21 | + | |
| 22 | + def augment(a, seen): | |
| 23 | + for b in edges[a]: | |
| 24 | + if b in seen: | |
| 25 | + continue | |
| 26 | + seen.add(b) | |
| 27 | + if b not in right_match or augment(right_match[b], seen): | |
| 28 | + left_match[a] = b | |
| 29 | + right_match[b] = a | |
| 30 | + return True | |
| 31 | + return False | |
| 32 | + | |
| 33 | + for a in range(len(left)): | |
| 34 | + augment(a, set()) | |
| 35 | + | |
| 36 | + # The complement of Konig's minimum vertex cover is a maximum independent set. | |
| 37 | + reachable_left = {a for a in range(len(left)) if a not in left_match} | |
| 38 | + reachable_right = set() | |
| 39 | + queue = sorted(reachable_left) | |
| 40 | + for a in queue: | |
| 41 | + for b in edges[a]: | |
| 42 | + if left_match.get(a) == b or b in reachable_right: | |
| 43 | + continue | |
| 44 | + reachable_right.add(b) | |
| 45 | + matched = right_match[b] | |
| 46 | + if matched not in reachable_left: | |
| 47 | + queue.append(matched) | |
| 48 | + reachable_left.add(matched) | |
| 49 | + | |
| 50 | + result = shared | {left[a] for a in reachable_left} | |
| 51 | + result.update(word for b, word in enumerate(right) if b not in reachable_right) | |
| 52 | + assert len(result) == len(shared) + len(left) + len(right) - len(right_match) | |
| 53 | + return result | |
| 54 | + | |
| 55 | + | |
| 56 | +def read_code(path): | |
| 57 | + data = json.loads(path.read_text(encoding="utf-8")) | |
| 58 | + n = data["n"] | |
| 59 | + words = set(data["words"]) | |
| 60 | + if not isinstance(n, int) or isinstance(n, bool) or n < 2 or len(words) != len(data["words"]): | |
| 61 | + raise ValueError(f"Expected a length and distinct words in {path}") | |
| 62 | + occupied = set() | |
| 63 | + for word in words: | |
| 64 | + if len(word) != n or set(word) - {"0", "1"}: | |
| 65 | + raise ValueError(f"Invalid word in {path}") | |
| 66 | + children = descendants(word) | |
| 67 | + if not occupied.isdisjoint(children): | |
| 68 | + raise ValueError(f"Conflicting codewords in {path}") | |
| 69 | + occupied.update(children) | |
| 70 | + return n, words | |
| 71 | + | |
| 72 | + | |
| 73 | +def best_order(codes): | |
| 74 | + best = set() | |
| 75 | + best_order = () | |
| 76 | + for order in permutations(range(len(codes))): | |
| 77 | + candidate = codes[order[0]] | |
| 78 | + for i in order[1:]: | |
| 79 | + candidate = combine(candidate, codes[i]) | |
| 80 | + if len(candidate) > len(best): | |
| 81 | + best, best_order = candidate, order | |
| 82 | + return best, best_order | |
| 83 | + | |
| 84 | + | |
| 85 | +def main(): | |
| 86 | + parser = argparse.ArgumentParser(description=__doc__) | |
| 87 | + parser.add_argument("output", type=Path) | |
| 88 | + parser.add_argument("codes", type=Path, nargs="+") | |
| 89 | + args = parser.parse_args() | |
| 90 | + if not 2 <= len(args.codes) <= 4: | |
| 91 | + parser.error("Supply between two and four valid codes of one length") | |
| 92 | + parsed = [read_code(path) for path in args.codes] | |
| 93 | + lengths = {n for n, _ in parsed} | |
| 94 | + if len(lengths) != 1: | |
| 95 | + parser.error("All input codes must have the same length") | |
| 96 | + n = lengths.pop() | |
| 97 | + codes = [words for _, words in parsed] | |
| 98 | + result, order = best_order(codes) | |
| 99 | + print("Input sizes:", ", ".join(str(len(code)) for code in codes)) | |
| 100 | + print("Winning order:", ", ".join(args.codes[i].name for i in order)) | |
| 101 | + print(f"Recombined {len(result)} words") | |
| 102 | + args.output.write_text(json.dumps({"n": n, "words": sorted(result)}, indent=2) + "\n", encoding="utf-8") | |
| 103 | + | |
| 104 | + | |
| 105 | +if __name__ == "__main__": | |
| 106 | + main() | |
