Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include <vector>
  2. #include <map>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. int biggest(int a, int b)
  12. {
  13.     if(a>b) return a;
  14.     return b;
  15. }
  16.  
  17. int smallest(int a,int b)
  18. {
  19.     if(a<b) return a;
  20.     return b;
  21. }
  22.  
  23. struct Point
  24. {
  25.     int x,y;
  26.     void print()
  27.     {
  28.         cout << "(" << x << "," << y << ")" << endl;
  29.     }
  30. };
  31.  
  32. struct Board
  33. {
  34.     int x0=0,x1=0,y0=0,y1=0;
  35.  
  36.     map<int,map<int,bool>> board;
  37.     void mergeSizes(const Board & board)
  38.     {
  39.         print();
  40.         x0=smallest(x0,board.x0);
  41.         x1=biggest(x1,board.x1);
  42.         y0=smallest(y0,board.y0);
  43.         y1=biggest(y1,board.y1);
  44.     }
  45.  
  46.     int getCenter(int a0, int a1)
  47.     {
  48.         return (a0+a1) / 2 ;
  49.     }
  50.  
  51. public:
  52.     int smallestManhattanDistanceFrom(int x, int y)
  53.     {
  54.         Point ret;
  55.         long smallestDist=9999999999;
  56.         for (const auto & e : board)
  57.         {
  58.             Point comp;
  59.             comp.x=e.first;
  60.             for(const auto & f : e.second)
  61.             {
  62.                 comp.y=f.first;
  63.                 if(f.second)
  64.                 {
  65.                     auto dist = abs(x-comp.x) + abs(y-comp.y);
  66.                     cout << "x: " << x << "y: " << y << "comp.x: " << comp.x << "comp.y: " << comp.y << " dist:" << dist << endl;
  67.  
  68.                     if(dist < smallestDist)
  69.                     {
  70.                         ret=comp;
  71.                         smallestDist=dist;
  72.                         cout << "Smallest: " << dist;
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         return smallestDist;
  78.     }
  79.  
  80.     void setPoint(int x, int y)
  81.     {
  82.         board[x][y] = true;
  83.         if(x<x0) x0=x;
  84.         if(x>x1) x1=x;
  85.         if(y<y0) y0=y;
  86.         if(y>y1) y1=y;
  87.     }
  88.  
  89.     int getCenterX()
  90.     {
  91.         return getCenter(x0,x1);
  92.     }
  93.  
  94.     int getCenterY()
  95.     {
  96.         return getCenter(y0,y1);
  97.     }
  98.  
  99.     bool getPoint(int x, int y) const
  100.     {
  101.         try {
  102.            return board.at(x).at(y);
  103.         }
  104.         catch (out_of_range) {
  105.             return false;
  106.         }
  107.     }
  108.  
  109.     void intersect(const Board & board_to_interserct)
  110.     {
  111.         Board intersect;
  112.  
  113.         int x,y;
  114.         for (const auto  e : board)
  115.         {
  116.             x=e.first;
  117.             for(const auto & f : e.second)
  118.             {
  119.                 y=f.first;
  120.                 if(!board_to_interserct.getPoint(x,y))
  121.                 {
  122.                    board[x][y]=false;
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.     void print()
  129.     {
  130.         int x,y;
  131.         for (auto e : board) {
  132.             x = e.first;
  133.             for (auto f : e.second) {
  134.                 y = f.first;
  135.                 if (f.second==true) cout << "(" << x << "," << y << ")" << endl;
  136.             }
  137.         }
  138.         printCoords();
  139.     }
  140.  
  141.     void printCoords()
  142.     {
  143.         cout
  144.                 << "x0:" << x0 << ", "
  145.                 << "x1:" << x1 << ", "
  146.                 << "y0:" << y0 << ", "
  147.                 << "y1:" << y1 << endl;
  148.     }
  149. };
  150.  
  151. class BoardManager
  152. {
  153.     vector<Board> boards;
  154.  
  155.     Board load_line(const string & line)
  156.     {
  157.         Board ret;
  158.         stringstream ss(line);
  159.         string pathInfo;
  160.         char c;
  161.         int x=0,y=0;
  162.         while (ss >> c)
  163.         {
  164.             int xincrement, yincrement;
  165.             if(c==',')
  166.             {
  167.                 long steps = stol(string(pathInfo.begin()+1,pathInfo.end()));
  168.                 xincrement=get_xincrement_from_char(pathInfo[0]);
  169.                 yincrement=get_yincrement_from_char(pathInfo[0]);
  170.  
  171.                 for (unsigned long i=0; i!=steps; ++i)
  172.                 {
  173.                     x += xincrement;
  174.                     y += yincrement;
  175.                     ret.setPoint(x,y);
  176.                 }
  177.  
  178.                 pathInfo="";
  179.             }
  180.             else
  181.             {
  182.                 pathInfo+=c;
  183.             }
  184.         }
  185.         return ret;
  186.     }
  187.  
  188.     int get_xincrement_from_char(char c)
  189.     {
  190.         if(c=='U') return -1;
  191.         if(c=='D') return 1;
  192.         return 0;
  193.     }
  194.  
  195.     int get_yincrement_from_char(char c)
  196.     {
  197.         if(c=='R') return  1;
  198.         if(c=='L') return -1;
  199.         return 0;
  200.     }
  201.  
  202. public:
  203.  
  204.     bool load_paths(string filename)
  205.     {
  206.         fstream fs(filename);
  207.         stringstream ss;
  208.         int currentx=0,currenty=0;
  209.         char c;
  210.         string line;
  211.         while(getline(fs,line))
  212.         {
  213.             boards.push_back(load_line(line));
  214.         }
  215.  
  216.         return  true;
  217.     }
  218.  
  219.     Board getIntersection()
  220.     {
  221.         if(boards.size()!=2) {
  222.             cout << "The number of boards is different than 2, can't intersect." << endl;
  223.         }
  224.         else {
  225.             auto intersection = boards[0];
  226.             intersection.intersect(boards[1]);
  227.             return intersection;
  228.         }
  229.     }
  230.  
  231. };
  232.  
  233.  
  234.  
  235. int main(int argc, char ** argv)
  236. {
  237.     if(argc != 2)
  238.     {
  239.         cout << "Please specify the a file with the list of paths" << endl;
  240.         return 1;
  241.     }
  242.     BoardManager boardManager;
  243.     boardManager.load_paths(argv[1]);
  244.     auto intersection = boardManager.getIntersection();
  245.     intersection.print();
  246.     intersection.smallestManhattanDistanceFrom(0,0);
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement