Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <map>
5 : : #include <set>
6 : : #include <string>
7 : : #include <array>
8 : :
9 : : using namespace std;
10 : :
11 : : static constexpr std::array<int, 2> dirFactors{-1, 1};
12 : :
13 : 2 : int main() {
14 : 2 : ifstream inputFile("input.txt");
15 [ + + ]: 2 : if (!inputFile) {
16 : 1 : cerr << "Failed to open input.txt";
17 : 1 : return 1;
18 : : }
19 : :
20 : 1 : map<char, vector<pair<int, int>>> antennas;
21 : 1 : int rows = 0;
22 : : int cols;
23 : 1 : set<pair<int, int>> antinodes;
24 : :
25 [ + + ]: 51 : for (string line; getline(inputFile, line); ++rows) {
26 : 50 : cols = static_cast<int>(line.size());
27 [ + + ]: 2550 : for (int x = 0; x < line.size(); ++x) {
28 [ + + ]: 2500 : if (line[x] != '.') {
29 : 135 : antennas[line[x]].emplace_back(x, rows);
30 : : }
31 : : }
32 : 1 : }
33 : :
34 [ + + ]: 37 : for (const auto& [freq, positions] : antennas) {
35 [ + + ]: 171 : for (size_t i = 0; i < positions.size(); ++i) {
36 [ + + ]: 324 : for (size_t j = i + 1; j < positions.size(); ++j) {
37 : 189 : int dx = positions[j].first - positions[i].first;
38 : 189 : int dy = positions[j].second - positions[i].second;
39 [ + + ]: 567 : for (int dir : dirFactors) {
40 : 378 : for (int a = 0; ; ++a) {
41 : 1896 : pair<int, int> p = { positions[i].first + dir * a * dx, positions[i].second + dir * a * dy };
42 [ + + + + : 1896 : if (p.first >= 0 && p.second >= 0 && p.first < cols && p.second < rows) {
+ + + + ]
43 : 1518 : antinodes.insert(p);
44 : : } else {
45 : : break;
46 : : }
47 : 1518 : }
48 : : }
49 : : }
50 : : }
51 : : }
52 : :
53 : 1 : cout << antinodes.size() << endl;
54 [ + + ]: 3 : }
|