ErnestGalbrun

Untitled

Dec 5th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1.  
  2. struct Point {
  3.     int x;
  4.     int y;
  5. };
  6.  
  7. struct Line {
  8.     Point start;
  9.     Point end;
  10. };
  11.  
  12. int main() {
  13.     auto start = absl::Now();
  14.  
  15.     vector<Line> lines;
  16.     regex r(R"((\d+),(\d+) -> (\d+),(\d+))");
  17.     smatch base_match;
  18.     for (std::string line; std::getline(std::cin, line);) {
  19.         regex_match(line, base_match, r);
  20.         lines.push_back({.start= {.x=stoi(base_match[1]), .y=stoi(base_match[2])},
  21.                         .end = {.x=stoi(base_match[3]), .y=stoi(base_match[4])}});
  22.     }
  23.  
  24.     int N = 1000;
  25.     std::vector<int> grid(N * N, 0);
  26.  
  27.     for (auto l: lines) {
  28.         int r = 0;
  29.         int c = 0;
  30.         int dr = l.start.x == l.end.x ? 0 : l.end.x > l.start.x ? 1 : -1;
  31.         int dc = l.start.y == l.end.y ? 0 : l.end.y > l.start.y ? 1 : -1;
  32.         for (r = l.start.x, c = l.start.y;
  33.              r != l.end.x + dr || c != l.end.y + dc; c += dc, r += dr) {
  34.             grid[r * N + c]++;
  35.         }
  36.     }
  37.  
  38.     int ans = absl::c_count_if(grid, [](auto n) { return n > 1; });
  39.  
  40.     cout << ans << endl;
  41.     cout << absl::Now() - start << endl;
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment