Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Point {
- int x;
- int y;
- };
- struct Line {
- Point start;
- Point end;
- };
- int main() {
- auto start = absl::Now();
- vector<Line> lines;
- regex r(R"((\d+),(\d+) -> (\d+),(\d+))");
- smatch base_match;
- for (std::string line; std::getline(std::cin, line);) {
- regex_match(line, base_match, r);
- lines.push_back({.start= {.x=stoi(base_match[1]), .y=stoi(base_match[2])},
- .end = {.x=stoi(base_match[3]), .y=stoi(base_match[4])}});
- }
- int N = 1000;
- std::vector<int> grid(N * N, 0);
- for (auto l: lines) {
- int r = 0;
- int c = 0;
- int dr = l.start.x == l.end.x ? 0 : l.end.x > l.start.x ? 1 : -1;
- int dc = l.start.y == l.end.y ? 0 : l.end.y > l.start.y ? 1 : -1;
- for (r = l.start.x, c = l.start.y;
- r != l.end.x + dr || c != l.end.y + dc; c += dc, r += dr) {
- grid[r * N + c]++;
- }
- }
- int ans = absl::c_count_if(grid, [](auto n) { return n > 1; });
- cout << ans << endl;
- cout << absl::Now() - start << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment