Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <unordered_map>
- #include <unordered_set>
- #include <sstream>
- #include <vector>
- #include <utility>
- struct pair_hash {
- template <class T1, class T2>
- std::size_t operator () (const std::pair<T1, T2>& p) const {
- auto h1 = std::hash<T1>{}(p.first);
- auto h2 = std::hash<T2>{}(p.second);
- return h1 ^ h2;
- }
- };
- void foldY(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points);
- void foldX(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points);
- void print(int x, int y, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points);
- bool pointExist(int w, int h, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points);
- int main()
- {
- std::ifstream file("Data.txt");
- std::string str;
- std::vector<std::pair<char, int>> fold;
- std::unordered_map<std::pair<int, int>, bool, pair_hash> points;
- //Get points
- while (std::getline(file, str)) {
- std::stringstream ss(str);
- std::string split;
- std::string s[2];
- int index = 0;
- if (str.find(',') != std::string::npos) {
- while (getline(ss, split, ',')) {
- s[index] = split;
- index++;
- }
- points.insert_or_assign(std::pair<int, int>(std::stoi(s[0]), std::stoi(s[1])), true);
- }
- else if ((str.find('=') != std::string::npos)) {
- while (getline(ss, split, '=')) {
- s[index] = split;
- index++;
- }
- fold.push_back(std::pair<char, int>(s[0][0], std::stoi(s[1])));
- }
- }
- for (auto currentFold : fold) {
- switch (currentFold.first) {
- case 'x':
- foldX(currentFold.second, points);
- break;
- case'y':
- foldY(currentFold.second, points);
- break;
- }
- }
- print(50, 10, points);
- }
- void foldY(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points) {
- std::vector <std::unordered_map<std::pair<int, int>, bool, pair_hash>::iterator> toDelete;
- int x, y;
- for (auto it = points.begin(); it != points.end(); it++) {
- x = it->first.first;
- y = it->first.second;
- if (y > value) {
- y = value - (it->first.second - value);
- points.insert_or_assign(std::pair<int, int>(x, y), true);
- toDelete.push_back(it);
- }
- }
- for (auto element : toDelete) points.erase(element);
- }
- void foldX(int value, std::unordered_map<std::pair<int, int>, bool, pair_hash>& points) {
- std::vector <std::unordered_map<std::pair<int, int>, bool, pair_hash>::iterator> toDelete;
- int x, y;
- for (auto it = points.begin(); it != points.end(); it++) {
- x = it->first.first;
- y = it->first.second;
- if (x > value) {
- x = value - (it->first.first- value);
- points.insert_or_assign(std::pair<int, int>(x, y), true);
- toDelete.push_back(it);
- }
- }
- for (auto element : toDelete) points.erase(element);
- }
- bool pointExist(int x, int y, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points) {
- auto checkExisting = points.find(std::pair<int, int>(x, y));
- if (checkExisting != points.end()) {
- return true;
- }
- return false;
- }
- void print(int w, int h, const std::unordered_map<std::pair<int, int>, bool, pair_hash> points) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- if (pointExist(x, y, points)) std::cout << "#";
- else std::cout << ".";
- }
- std::cout << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement