#include #include #include #include constexpr char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; constexpr size_t alphabet_len = 26; static std::map, char> flags = { {{5,6}, 'a'}, {{5,7}, 'b'}, {{5,8}, 'c'}, {{1,5}, 'd'}, {{2,5}, 'e'}, {{3,5}, 'f'}, {{4,5}, 'g'}, {{6,7}, 'h'}, {{6,8}, 'i'}, {{1,3}, 'j'}, {{1,6}, 'k'}, {{2,6}, 'l'}, {{3,6}, 'm'}, {{4,6}, 'n'}, {{7,8}, 'o'}, {{1,7}, 'p'}, {{2,7}, 'q'}, {{3,7}, 'r'}, {{4,7}, 's'}, {{1,8}, 't'}, {{2,8}, 'u'}, {{1,4}, 'v'}, {{2,3}, 'w'}, {{2,4}, 'x'}, {{3,8}, 'y'}, {{3,4}, 'z'}, }; static std::map> alpha = { {'a', {5,6}}, {'b', {5,7}}, {'c', {5,8}}, {'d', {1,5}}, {'e', {2,5}}, {'f', {3,5}}, {'g', {4,5}}, {'h', {6,7}}, {'i', {6,8}}, {'j', {1,3}}, {'k', {1,6}}, {'l', {2,6}}, {'m', {3,6}}, {'n', {4,6}}, {'o', {7,8}}, {'p', {1,7}}, {'q', {2,7}}, {'r', {3,7}}, {'s', {4,7}}, {'t', {1,8}}, {'u', {2,8}}, {'v', {1,4}}, {'w', {2,3}}, {'x', {2,4}}, {'y', {3,8}}, {'z', {3,4}}, }; size_t letter_to_index(char c) { return c - 'a'; } char decodePosition(int pos1, int pos2) { return flags[{std::min(pos1, pos2), std::max(pos1, pos2)}]; }; char decodeLetter() { std::vector positions; std::string line; for (int i = 0; i < 9; i++) { std::getline(std::cin, line); switch (i) { case 1: if (line[1] == '#') positions.push_back(8); if (line[4] == '#') positions.push_back(1); if (line[7] == '#') positions.push_back(2); break; case 4: if (line[1] == '#') positions.push_back(7); if (line[7] == '#') positions.push_back(3); break; case 7: if (line[1] == '#') positions.push_back(6); if (line[4] == '#') positions.push_back(5); if (line[7] == '#') positions.push_back(4); break; } } if (positions.size() != 2) throw std::invalid_argument("Didn't detect two flags"); return decodePosition(positions[0], positions[1]); } std::string decodeMessage(size_t length) { std::string message; message.reserve(length); for (size_t i = 0; i < length; i++) { char letter = decodeLetter(); message.push_back(letter); } return message; } void shiftMessage(std::string &message, int shift) { for (char &c : message) { size_t index = letter_to_index(c); size_t shifted = (index + shift) % alphabet_len; c = alphabet[shifted]; } } std::pair getPosition(char c) { return alpha[c]; } bool hasPos(std::pair &pos, int query) { return pos.first == query || pos.second == query; } void printLetter(char c) { auto pos = getPosition(c); for (size_t i = 0; i < 9; i++) { switch (i) { case 0: case 8: std::cout << "........."; break; case 1: if (hasPos(pos, 8)) std::cout << ".#.."; else std::cout << "...."; if (hasPos(pos, 1)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 2)) std::cout << "..#."; else std::cout << "...."; break; case 2: if (hasPos(pos, 8)) std::cout << "..#."; else std::cout << "...."; if (hasPos(pos, 1)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 2)) std::cout << ".#.."; else std::cout << "...."; break; case 3: if (hasPos(pos, 8)) std::cout << "...#"; else std::cout << "...."; if (hasPos(pos, 1)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 2)) std::cout << "#..."; else std::cout << "...."; break; case 4: if (hasPos(pos, 7)) std::cout << ".###"; else std::cout << "...."; std::cout << "*"; if (hasPos(pos, 3)) std::cout << "###."; else std::cout << "...."; break; case 5: if (hasPos(pos, 6)) std::cout << "...#"; else std::cout << "...."; if (hasPos(pos, 5)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 4)) std::cout << "#..."; else std::cout << "...."; break; case 6: if (hasPos(pos, 6)) std::cout << "..#."; else std::cout << "...."; if (hasPos(pos, 5)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 4)) std::cout << ".#.."; else std::cout << "...."; break; case 7: if (hasPos(pos, 6)) std::cout << ".#.."; else std::cout << "...."; if (hasPos(pos, 5)) std::cout << "#"; else std::cout << "."; if (hasPos(pos, 4)) std::cout << "..#."; else std::cout << "...."; break; } std::cout << std::endl; } } void printMessage(std::string &message) { for (char c : message) { // std::cout << c << std::endl; printLetter(c); } } void solveMessage() { size_t length, shift; std::cin >> length >> shift; // Remove newline std::cin.get(); std::string msg = decodeMessage(length); // std::cout << msg << std::endl; shiftMessage(msg, shift); // std::cout << msg << std::endl; printMessage(msg); } int main() { // std::string msg = "abcdefghijklmnopqrstuvwxyz"; // printMessage(msg); solveMessage(); return 0; }