Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <tuple>
5 : : #include <string>
6 : : #include <cstdint>
7 : : #include <cstdio>
8 : : #include <algorithm>
9 : : using namespace std;
10 : :
11 : 2 : int main() {
12 : 2 : const int width = 101;
13 : 2 : const int height = 103;
14 : 2 : int seconds = 0;
15 : : int px;
16 : : int py;
17 : : int vx;
18 : : int vy;
19 : :
20 : 2 : vector<tuple<int, int, int, int>> robots; // (px, py, vx, vy)
21 : 2 : ifstream inputFile("input.txt");
22 : :
23 [ + + ]: 2 : if (!inputFile) {
24 : 1 : cerr << "Error: Could not open input.txt" << endl;
25 : 1 : return 1;
26 : : }
27 : :
28 [ + + ]: 501 : for (string line; getline(inputFile, line); ) {
29 : 500 : sscanf(line.c_str(), "p=%d,%d v=%d,%d", &px, &py, &vx, &vy);
30 : 500 : robots.emplace_back(px, py, vx, vy);
31 : 1 : }
32 : :
33 : 1 : vector<uint8_t> map(height * width, false);
34 : :
35 : 1 : for (++seconds; ; ++seconds) {
36 : 6532 : ranges::fill(map, 0);
37 : :
38 : 6532 : bool collision = false;
39 [ + + ]: 823693 : for (const auto &[robotPx, robotPy, robotVx, robotVy] : robots) {
40 : 823692 : int x = robotPx + seconds * robotVx;
41 : 823692 : int y = robotPy + seconds * robotVy;
42 : :
43 : 823692 : x %= width;
44 [ + + ]: 823692 : if(x < 0) {
45 : 444512 : x += width;
46 : : }
47 : 823692 : y %= height;
48 [ + + ]: 823692 : if(y < 0) {
49 : 412334 : y += height;
50 : : }
51 : :
52 : 823692 : int index = y * width + x;
53 : :
54 [ + + ]: 823692 : if (map[index]) {
55 : 6531 : collision = true;
56 : 6531 : break;
57 : : }
58 : 817161 : map[index] = 1;
59 : : }
60 : :
61 [ + + ]: 6532 : if (!collision) {
62 : 1 : break;
63 : : }
64 : 6531 : }
65 : :
66 : 1 : cout << seconds << endl;
67 [ + + + + ]: 4 : }
|