Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <string>
4 : : #include <vector>
5 : : #include <set>
6 : : #include <array>
7 : : using namespace std;
8 : :
9 : 2 : int main() {
10 : 2 : ifstream inputFile("input.txt");
11 [ + + ]: 2 : if (!inputFile) {
12 : 1 : cerr << "Error opening file." << endl;
13 : 1 : return 1;
14 : : }
15 : :
16 : 1 : vector<string> map;
17 : 1 : int direction = 0;
18 : 1 : int x = 0;
19 : 1 : int y = 0;
20 : :
21 [ + + ]: 131 : for (string line; getline(inputFile, line); map.push_back(std::move(line))) {
22 [ + + ]: 130 : if (auto pos = line.find('^'); pos != string::npos) {
23 : 1 : x = static_cast<int>(pos);
24 : 1 : y = static_cast<int>(map.size());
25 : : }
26 : 1 : }
27 : :
28 : 2 : set<pair<int, int>> visited{ {x, y} };
29 : 1 : constexpr array<int, 4> directionOffsets{0, 1, 0, -1};
30 : :
31 : : while (true) {
32 : 5786 : int nx = x + directionOffsets[direction];
33 : 5786 : int ny = y + directionOffsets[(direction + 3) & 3];
34 [ + - + - : 5786 : if (ny < 0 || ny >= map.size() || nx < 0 || nx >= map[0].size()) {
+ - + + +
+ ]
35 : 1 : break;
36 [ + + ]: 5785 : } else if (map[ny][nx] == '#') {
37 : 153 : direction = (direction + 1) & 3;
38 : : } else {
39 : 5632 : visited.insert({ x = nx, y = ny });
40 : : }
41 : 5785 : }
42 : :
43 : 1 : cout << visited.size() << endl;
44 [ + + ]: 3 : }
|