Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- struct Point {
- int x;
- int y;
- auto operator<=>(const Point &) const = default;
- auto operator+(const Point &b) const {
- return Point{.x = x + b.x, .y = y + b.y};
- }
- void operator+=(const Point &b) {
- x += b.x;
- y += b.y;
- }
- struct Hash {
- size_t operator()(const Point &p) const {
- size_t rowHash = std::hash<int>()(p.x);
- size_t colHash = std::hash<int>()(p.y) << 1;
- return rowHash ^ colHash;
- }
- };
- };
- void print(unordered_set<Point, Point::Hash> P) {
- int X = max_element(P.begin(), P.end(), [](auto p, auto t){return p.x < t.x;})->x;
- int Y = max_element(P.begin(), P.end(), [](auto p, auto t){return p.y < t.y;})->y;
- for (int r = 0; r <= Y; ++r) {
- for (int c = 0; c <= X; ++c) {
- cout << (P.contains({.x= c, .y = r}) ? 'X' : '.');
- }
- cout << endl;
- }
- cout << endl;
- }
- int main() {
- auto start = absl::Now();
- unordered_set<Point, Point::Hash> P;
- for (std::string line; std::getline(std::cin, line);) {
- if (line.empty()) break;
- vector<string> p = absl::StrSplit(line, ",");
- P.insert({.x = stoi(p[0]), .y = stoi(p[1])});
- }
- regex r(R"(fold along ([xy])=(\d+))");
- smatch base_match;
- for (std::string line; std::getline(std::cin, line);) {
- regex_match(line, base_match, r);
- bool x = base_match[1] == 'x';
- int n = stoi(base_match[2]);
- auto P2 = P;
- for (auto &p: P) {
- if (x) {
- if (p.x == n) {
- P2.erase(p);
- }
- if (p.x > n) {
- P2.erase(p);
- P2.insert({.x = n - (p.x - n), .y= p.y});
- }
- } else {
- if (p.y == n) {
- P2.erase(p);
- } else if (p.y > n) {
- P2.erase(p);
- P2.insert({.x = p.x, .y= n - (p.y - n)});
- }
- }
- }
- P = P2;
- }
- print(P);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment