|
| 1 | +#pragma once |
| 2 | +#include "../../terminal.cpp" |
| 3 | +using namespace std; |
| 4 | +#define padding 3 |
| 5 | + |
| 6 | +struct pattern { |
| 7 | + vector<vector<bool>> grid; |
| 8 | + vector<int> birth = {3}; |
| 9 | + vector<int> survive = {2, 3}; |
| 10 | +}; |
| 11 | + |
| 12 | +class decode { |
| 13 | +public: |
| 14 | + static pattern cells(string patternCode){ |
| 15 | + return text(patternCode, 'O', '.', '!'); |
| 16 | + } |
| 17 | + static pattern rle(string patternCode) { |
| 18 | + while (patternCode[0] == '#') patternCode = patternCode.substr(patternCode.find('\n') + 1); |
| 19 | + pattern p; |
| 20 | + int x = 0, y = 0; |
| 21 | + size_t pos = patternCode.find("\n"); |
| 22 | + if (pos == string::npos) { |
| 23 | + wcout << "Invalid RLE: Missing header"; |
| 24 | + terminal::exit(); |
| 25 | + } |
| 26 | + string header = patternCode.substr(0, pos++); |
| 27 | + size_t xPos = header.find("x = "), |
| 28 | + yPos = header.find("y = "), |
| 29 | + rulePos = header.find("rule = "); |
| 30 | + if (xPos == string::npos || yPos == string::npos) { |
| 31 | + wcout << "Invalid RLE: Missing dimensions"; |
| 32 | + terminal::exit(); |
| 33 | + } |
| 34 | + x = stoi(header.substr(xPos + 4)); |
| 35 | + y = stoi(header.substr(yPos + 4, header.find(',', yPos) - yPos - 4)); |
| 36 | + p.grid.resize(y, vector<bool>(x, 0)); |
| 37 | + if (rulePos != string::npos) { |
| 38 | + p.birth = {}, p.survive = {}; |
| 39 | + string ruleStr = header.substr(rulePos + 7); |
| 40 | + size_t slashPos = ruleStr.find('/'); |
| 41 | + string birthStr = ruleStr.substr(0, slashPos); |
| 42 | + string surviveStr = ruleStr.substr(slashPos + 1); |
| 43 | + if (!isdigit(birthStr[0])) birthStr = birthStr.substr(1); |
| 44 | + if (!isdigit(surviveStr[0])) surviveStr = surviveStr.substr(1); |
| 45 | + else swap(birthStr, surviveStr); |
| 46 | + for (char c: birthStr) p.birth.emplace_back(c - 48); |
| 47 | + for (char c: surviveStr) if (isdigit(c)) p.survive.emplace_back(c - 48); |
| 48 | + } |
| 49 | + int currentRow = 0, |
| 50 | + currentCol = 0, |
| 51 | + count = 0; |
| 52 | + char c; |
| 53 | + for (; pos < patternCode.size() && patternCode[pos] != '!'; pos++) { |
| 54 | + c = patternCode[pos]; |
| 55 | + if (isdigit(c)) count = count * 10 + (c - 48); |
| 56 | + else { |
| 57 | + if (count == 0) count = 1; |
| 58 | + switch (c) { |
| 59 | + case 'b': for (int i = 0; i < count; i++) p.grid[currentRow][currentCol++] = 0; break; |
| 60 | + case 'o': for (int i = 0; i < count; i++) p.grid[currentRow][currentCol++] = 1; break; |
| 61 | + case '$': currentRow += count, currentCol = 0; break; |
| 62 | + } |
| 63 | + count = 0; |
| 64 | + } |
| 65 | + } |
| 66 | + pad(p, padding); |
| 67 | + return p; |
| 68 | + } |
| 69 | +private: |
| 70 | + static pattern text(string patternCode, char alive, const char dead, char comment){ |
| 71 | + while (patternCode[0] == comment) patternCode = patternCode.substr(patternCode.find('\n') + 1); |
| 72 | + int width = patternCode.find('\n') + 1; |
| 73 | + pattern p; |
| 74 | + p.grid.emplace_back(vector<bool>()); |
| 75 | + for (char c: patternCode){ |
| 76 | + if (c == dead) p.grid.back().emplace_back(0); |
| 77 | + else if (c == alive) p.grid.back().emplace_back(1); |
| 78 | + else if (c == '\n'){ |
| 79 | + p.grid.emplace_back(vector<bool>()); |
| 80 | + } |
| 81 | + } |
| 82 | + pad(p, padding); |
| 83 | + return p; |
| 84 | + } |
| 85 | + static void pad(pattern &p, int pad){ |
| 86 | + int maxWidth = 0; |
| 87 | + for (auto i: p.grid) maxWidth = max(maxWidth, int(i.size())); |
| 88 | + for (int j = 0; j < pad; j++) p.grid.emplace(p.grid.begin(), vector<bool>(maxWidth + 2 * pad, 0)); |
| 89 | + for (auto &i: p.grid) { |
| 90 | + for (int j = 0; j < pad; j++) i.emplace(i.begin(), 0); |
| 91 | + i.resize(maxWidth + 2 * pad, 0); |
| 92 | + } |
| 93 | + p.grid.resize(p.grid.size() + 2 * pad, vector<bool>(maxWidth + 2 * pad)); |
| 94 | + } |
| 95 | +}; |
0 commit comments