#include #include #include #include #include using namespace std; const string ALPHA = "ogrc"; int main() { array state; iota(state.begin(), state.end(), 0); vector> moves; auto rot = [&](int idx, int amt) { vector indices; if(idx < 3) { if(amt < 0) moves.push_back({idx, amt + 10}); else moves.push_back({idx, amt}); amt %= 10; if(amt < 0) amt += 10; } else { assert(idx == 3); assert(amt == -1 || amt == 1); if(amt == 1) moves.push_back({3, 1}); else moves.push_back({3, 2}); amt = moves.back()[1] * 3; } if(idx <= 2) { indices.resize(10); iota(indices.begin(), indices.end(), 10 * idx); } else { assert(idx == 3); indices.resize(9); indices[0] = 3-1; indices[1] = 2-1; indices[2] = 1-1; indices[3] = 20+3-1; indices[4] = 20+2-1; indices[5] = 20+1-1; indices[6] = 10+3-1; indices[7] = 10+2-1; indices[8] = 10+1-1; } vector vals(indices.size()); for(int i = 0; i < vals.size(); i++) vals[i] = state[indices[i]]; for(int i = 0; i < vals.size(); i++) state[indices[(i+amt+indices.size())%indices.size()]] = vals[i]; }; auto swap_2_10 = [&]() -> void { rot(0, 1); rot(3, 1); rot(0, -1); rot(3, -1); }; auto swap_12_20 = [&]() -> void { rot(1, 1); rot(3, 1); rot(1, -1); rot(3, -1); }; auto swap_0_22 = [&]() -> void { rot(2, 1); rot(3, 1); rot(2, -1); rot(3, -1); }; for(int i = 0; i < 3; i++) { string s; cin >> s; for(int j = 0; j < 10; j++) { if(s[j] == 'o') state[10*i+j] = 0; else if(s[j] == 'g') state[10*i+j] = 1; else if(s[j] == 'r') state[10*i+j] = 2; else assert(false); } } auto done = [&]() -> bool { for(int i = 0; i < 30; i++) if(state[i] != i / 10) return false; return true; }; auto push = [&](int idx, int dst) -> void { assert(idx / 10 == dst / 10); int need = (dst - idx); if(need < 0) need += 10; if(need) { rot(idx / 10, need); } }; while(!done()) { assert(moves.size() < 800); int badidx = -1; for(int i = 0; badidx == -1 && i < 30; i++) { if(state[i] != i / 10) badidx = i; } assert(badidx >= 0); int dst = state[badidx]; int badidx2 = -1; for(int i = 0; i < 10; i++) { if(state[(10*dst)+i] != dst) badidx2 = 10*dst+i; } assert(badidx / 10 != badidx2 / 10); if(badidx > badidx2) swap(badidx, badidx2); if(badidx / 10 == 0 && badidx2 / 10 == 1) { push(badidx, 2); push(badidx2, 10); swap_2_10(); } else if(badidx / 10 == 0 && badidx2 / 10 == 2) { push(badidx, 0); push(badidx2, 22); swap_0_22(); } else if(badidx / 10 == 1 && badidx2 / 10 == 2) { push(badidx, 12); push(badidx2, 20); swap_12_20(); } else { assert(false); } } cout << moves.size() << "\n"; for(auto [idx, amt]: moves) cout << ALPHA[idx] << "" << amt << "\n"; }