Advertisement
Guest User

AoC2021d13

a guest
Dec 13th, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <unordered_map>
  5. #include <unordered_set>
  6. #include <sstream>
  7. #include <vector>
  8. #include <utility>
  9.  
  10.  
  11.  
  12. struct pair_hash {
  13.     template <class T1, class T2>
  14.     std::size_t operator () (const std::pair<T1, T2>& p) const {
  15.         auto h1 = std::hash<T1>{}(p.first);
  16.         auto h2 = std::hash<T2>{}(p.second);
  17.         return h1 ^ h2;
  18.     }
  19. };
  20.  
  21. void foldY(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points);
  22. void foldX(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points);
  23. void print(int x, int y, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points);
  24. bool pointExist(int w, int h, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points);
  25.  
  26. int main()
  27. {
  28.     std::ifstream file("Data.txt");
  29.     std::string str;
  30.     std::vector<std::pair<char, int>> fold;
  31.     std::unordered_map<std::pair<int, int>, bool, pair_hash> points;
  32.  
  33.  
  34.     //Get points
  35.     while (std::getline(file, str)) {
  36.         std::stringstream ss(str);
  37.         std::string split;
  38.  
  39.         std::string s[2];
  40.         int index = 0;
  41.  
  42.         if (str.find(',') != std::string::npos) {
  43.             while (getline(ss, split, ',')) {
  44.                 s[index] = split;
  45.                 index++;
  46.             }
  47.             points.insert_or_assign(std::pair<int, int>(std::stoi(s[0]), std::stoi(s[1])), true);
  48.         }
  49.         else if ((str.find('=') != std::string::npos)) {
  50.             while (getline(ss, split, '=')) {
  51.                 s[index] = split;
  52.                 index++;
  53.             }
  54.             fold.push_back(std::pair<char, int>(s[0][0], std::stoi(s[1])));
  55.         }
  56.     }
  57.  
  58.     for (auto currentFold : fold) {
  59.         switch (currentFold.first) {
  60.         case 'x':
  61.             foldX(currentFold.second, points);
  62.             break;
  63.         case'y':
  64.             foldY(currentFold.second, points);
  65.             break;
  66.         }
  67.     }
  68.  
  69.     print(50, 10, points);
  70. }
  71.  
  72. void foldY(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points) {
  73.     std::vector <std::unordered_map<std::pair<int, int>, bool, pair_hash>::iterator> toDelete;
  74.  
  75.     int x, y;
  76.     for (auto it = points.begin(); it != points.end(); it++) {
  77.         x = it->first.first;
  78.         y = it->first.second;
  79.  
  80.         if (y > value) {
  81.             y = value - (it->first.second - value);
  82.             points.insert_or_assign(std::pair<int, int>(x, y), true);
  83.             toDelete.push_back(it);
  84.         }
  85.     }
  86.  
  87.     for (auto element : toDelete) points.erase(element);
  88. }
  89.  
  90. void foldX(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points) {
  91.     std::vector <std::unordered_map<std::pair<int, int>, bool, pair_hash>::iterator> toDelete;
  92.  
  93.     int x, y;
  94.     for (auto it = points.begin(); it != points.end(); it++) {
  95.         x = it->first.first;
  96.         y = it->first.second;
  97.  
  98.         if (x > value) {
  99.             x = value - (it->first.first- value);
  100.             points.insert_or_assign(std::pair<int, int>(x, y), true);
  101.             toDelete.push_back(it);
  102.         }
  103.     }
  104.  
  105.     for (auto element : toDelete) points.erase(element);
  106. }
  107.  
  108. bool pointExist(int x, int y, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points) {
  109.     auto checkExisting = points.find(std::pair<int, int>(x, y));
  110.     if (checkExisting != points.end()) {
  111.         return true;
  112.     }
  113.     return false;
  114. }
  115.  
  116. void print(int w, int h, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points) {
  117.     for (int y = 0; y < h; y++) {
  118.         for (int x = 0; x < w; x++) {
  119.             if (pointExist(x, y, points)) std::cout << "#";
  120.             else std::cout << ".";
  121.         }
  122.         std::cout << std::endl;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement