ErnestGalbrun

Untitled

Dec 12th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. using namespace std;
  2.  
  3. struct Point {
  4.     int x;
  5.     int y;
  6.  
  7.     auto operator<=>(const Point &) const = default;
  8.  
  9.     auto operator+(const Point &b) const {
  10.         return Point{.x = x + b.x, .y = y + b.y};
  11.     }
  12.  
  13.     void operator+=(const Point &b) {
  14.         x += b.x;
  15.         y += b.y;
  16.     }
  17.  
  18.     struct Hash {
  19.         size_t operator()(const Point &p) const {
  20.             size_t rowHash = std::hash<int>()(p.x);
  21.             size_t colHash = std::hash<int>()(p.y) << 1;
  22.             return rowHash ^ colHash;
  23.         }
  24.     };
  25. };
  26.  
  27. void print(unordered_set<Point, Point::Hash> P) {
  28.     int X = max_element(P.begin(), P.end(), [](auto p, auto t){return p.x < t.x;})->x;
  29.     int Y = max_element(P.begin(), P.end(), [](auto p, auto t){return p.y < t.y;})->y;
  30.     for (int r = 0; r <= Y; ++r) {
  31.         for (int c = 0; c <= X; ++c) {
  32.             cout << (P.contains({.x= c, .y = r}) ? 'X' : '.');
  33.         }
  34.         cout << endl;
  35.     }
  36.     cout << endl;
  37. }
  38.  
  39. int main() {
  40.     auto start = absl::Now();
  41.  
  42.     unordered_set<Point, Point::Hash> P;
  43.     for (std::string line; std::getline(std::cin, line);) {
  44.         if (line.empty()) break;
  45.         vector<string> p = absl::StrSplit(line, ",");
  46.         P.insert({.x = stoi(p[0]), .y = stoi(p[1])});
  47.     }
  48.  
  49.     regex r(R"(fold along ([xy])=(\d+))");
  50.     smatch base_match;
  51.     for (std::string line; std::getline(std::cin, line);) {
  52.         regex_match(line, base_match, r);
  53.         bool x = base_match[1] == 'x';
  54.         int n = stoi(base_match[2]);
  55.  
  56.         auto P2 = P;
  57.         for (auto &p: P) {
  58.             if (x) {
  59.                 if (p.x == n) {
  60.                     P2.erase(p);
  61.                 }
  62.                 if (p.x > n) {
  63.                     P2.erase(p);
  64.                     P2.insert({.x = n - (p.x - n), .y= p.y});
  65.                 }
  66.             } else {
  67.                 if (p.y == n) {
  68.                     P2.erase(p);
  69.                 } else if (p.y > n) {
  70.                     P2.erase(p);
  71.                     P2.insert({.x = p.x, .y= n - (p.y - n)});
  72.                 }
  73.             }
  74.         }
  75.         P = P2;
  76.     }
  77.     print(P);
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment