Advertisement
Guest User

Untitled

a guest
Dec 5th, 2021
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include "Day5.h"
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <numeric>
  8. #include <cmath>
  9. struct Point
  10. {
  11.     int x = 0;
  12.     int y = 0;
  13. };
  14.  
  15. //returns -1 or 0 or 1
  16. static int IntComp_(int a, int b)
  17. {
  18.     return (b < a) - (a < b) ;
  19. }
  20.  
  21. class Line
  22. {
  23. public:
  24.     bool IsVertical() const { return p1.y == p2.y; }
  25.     bool IsHorizontal() const { return p1.x == p2.x; }
  26.     std::vector<Point> PointsBetween() const
  27.     {
  28.         std::vector<Point> pts;
  29.  
  30.         //find direction of x and y
  31.         auto dx = IntComp_(p2.x, p1.x);
  32.         auto dy = IntComp_(p2.y, p1.y);
  33.        
  34.         for (auto x = p1.x, y = p1.y;
  35.             x != p2.x || y != p2.y;
  36.             x += dx,y += dy)
  37.         {
  38.             pts.emplace_back(x, y);
  39.         }
  40.         pts.emplace_back(p2); //loop didn't include last point.
  41.         return pts;
  42.     }
  43.     Point p1;
  44.     Point p2;
  45. };
  46.  
  47. static std::pair<std::vector<Line>, int> ReadLines_()
  48. {
  49.     int maxSize = 0;
  50.     std::ifstream infile("5.txt");
  51.     std::vector<Line> lines;
  52.     Line line;
  53.     std::string filler;
  54.     char c1, c2;
  55.     while (infile >> line.p1.x >> c1 >> line.p1.y >> filler >> line.p2.x >> c2 >> line.p2.y)
  56.     {
  57.         maxSize = std::max({ maxSize, line.p1.x, line.p1.y, line.p2.x, line.p2.y });
  58.         lines.emplace_back(line);
  59.     }
  60.     return std::make_pair(lines, maxSize);
  61. }
  62.  
  63. void day5::A()
  64. {
  65.     auto [lines, maxSize] = ReadLines_();
  66.     std::vector<int> counts;
  67.     counts.resize(maxSize* maxSize);//1000x1000 = 1,000,000
  68.  
  69.     for(const auto& line : lines)
  70.     {
  71.         if (line.IsVertical() || line.IsHorizontal()) //skip diagonal
  72.         {
  73.             for (const auto pt : line.PointsBetween())
  74.             {
  75.                 auto index = pt.y * maxSize + pt.x;
  76.                 ++counts[index];
  77.             }
  78.         }
  79.     }
  80.     std::cout << "5A:" << std::count_if(counts.begin(), counts.end(), [](int i) {return i > 1; }) << "\n";
  81. }
  82.  
  83. void day5::B()
  84. {
  85.     auto [lines, maxSize] = ReadLines_();
  86.     std::vector<int> counts;
  87.     counts.resize(maxSize * maxSize);//1000x1000 = 1,000,000
  88.  
  89.     for (const auto& line : lines)
  90.     {
  91.         for (const auto pt : line.PointsBetween())
  92.         {
  93.             auto index = pt.y * maxSize + pt.x;
  94.             ++counts[index];
  95.         }
  96.     }
  97.     std::cout << "5A:" << std::count_if(counts.begin(), counts.end(), [](int i) {return i > 1; }); << "\n";
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement