#include "r8veritas.hh" #include #include #include #include #include #include #include r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { r8veritas::stat inp = generate_empty_stat(); r8veritas::stat out = generate_empty_stat(); for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { inp[i] = i; out[i] = operation(r8veritas::cell(i)); } return (r8veritas::truthtable(inp, out)); } std::pair operate_ins(const r8veritas::stat &last_stat, const r8veritas::ins &ins) { auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins)); r8veritas::stat next_stat = last_stat; auto it = visited_stat.find(key); if (it != visited_stat.end()) { // foud in visited_stat return {it->second, true}; } int opcode = ins.bc & 1; r8veritas::cell cell_mask = (ins.bc >> 1); for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { if ((next_stat[i] & (1 << ins.sacrifice)) != 0) { next_stat[i] ^= (1 << ins.sacrifice); if (opcode == OPCODE_SET1) { next_stat[i] |= cell_mask; } else if (opcode == OPCODE_FLIP) { next_stat[i] ^= cell_mask; } } } visited_stat.insert(std::pair(key, next_stat)); visited_op.insert(std::pair(next_stat, std::move(key))); return {next_stat, false}; } std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s) { for (auto i : s) { os << std::bitset(i).to_string() << " "; } return (os); } std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) { auto str = std::bitset(s.bc).to_string(); std::reverse(str.begin(), str.end()); os << s.bc << ":{"; if ((s.bc != 0b0) && (s.bc != 0b1)) { os << "OPERATION:"; if (str[0] == '0') os << "SET1: "; else os << "FLIP: "; for (r8veritas::index i = 1; i < str.length(); i++) { if (str[i] == '1') os << i << " "; } } os << ",SACRIFICE:" << s.sacrifice << "}"; return (os); } r8veritas::truthtable compress_stat_space(const r8veritas::truthtable &raw_truthtable) { std::array influences; r8veritas::truthtable new_truthtable = raw_truthtable; for (r8veritas::index i = 0; i < CELL_SIZE; i++) { influences[i] = 0; for (r8veritas::index j = 0; j < (1 << CELL_SIZE); j++) { if (raw_truthtable.second[j] != raw_truthtable.second[j ^ (1 << i)]) { influences[i]++; } } influences[i] >>= 1; } for (r8veritas::index i = 0; i < CELL_SIZE; i++) { if (influences[i] == 0) { new_truthtable.first = operate_ins(new_truthtable.first, {0, i}).first; } } return (new_truthtable); } static std::queue stats; bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { if (layer == 1) { stats.push(truthtable.first); } if (stats.empty()) { return (false); } r8veritas::index i = stats.size(); std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; r8veritas::stat tmp_stat; std::pair tmp_ans; auto operated = false; for (; i > 0; i--) { tmp_stat = stats.front(); for (auto j : r8veritas::all_ins) { tmp_ans = operate_ins(tmp_stat, j); if (tmp_ans.first == truthtable.second) { return (true); } if (!(tmp_ans.second)) { stats.push(tmp_ans.first); } } stats.pop(); } return (bfs(truthtable, layer + 1)); } r8veritas::path track_path(const r8veritas::truthtable &truthtable) { r8veritas::path p; for (auto tmp_stat = truthtable.second; tmp_stat != truthtable.first;) { p.push(visited_op[tmp_stat]); tmp_stat = p.top().first; std::cout << r8veritas::ins(p.top().second) << std::endl; } return p; }