Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <string>
5 : : #include <iterator>
6 : : #include <array>
7 : : using namespace std;
8 : :
9 : : constexpr string TARGET = "XMAS";
10 : : constexpr array<array<int, 2>, 8> directions{{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}};
11 : :
12 : 2 : int main() {
13 : 2 : ifstream inputFile("input.txt");
14 [ + + ]: 2 : if (!inputFile) {
15 : 1 : cerr << "Error: Could not open input file.\n";
16 : 1 : return 1;
17 : : }
18 : 3 : vector<string> grid{istream_iterator<string>{inputFile}, istream_iterator<string>{}};
19 : 1 : auto rows = static_cast<int>(grid.size());
20 : 1 : auto cols = static_cast<int>(grid[0].size());
21 : 1 : int count = 0;
22 : :
23 [ + + ]: 141 : for (int row = 0; row < rows; ++row) {
24 [ + + ]: 19740 : for (int col = 0; col < cols; ++col) {
25 [ + + ]: 176400 : for (auto [dx, dy] : directions) {
26 : 156800 : int x = row;
27 : 156800 : int y = col;
28 : 156800 : int k = 0;
29 [ + + + + : 206404 : for (; k < TARGET.size() && x >= 0 && x < rows && y >= 0 && y < cols && grid[x][y] == TARGET[k]; ++k) {
+ + + + +
+ + + +
+ ]
30 : 49604 : x += dx; y += dy;
31 : : }
32 [ + + ]: 156800 : if (k == TARGET.size()) {
33 : 2654 : ++count;
34 : : }
35 : : }
36 : : }
37 : : }
38 : :
39 : 1 : cout << count << endl;
40 [ + + ]: 3 : }
|