Branch data Line data Source code
1 : : #include <array>
2 : : #include <iostream>
3 : : #include <fstream>
4 : : #include <map>
5 : : #include <set>
6 : : #include <vector>
7 : : #include <string>
8 : :
9 : : using namespace std;
10 : :
11 : : static constexpr std::array<int, 2> dirFactors{-1, 2};
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 = 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 : pair<int, int> p = { positions[i].first + dir * dx, positions[i].second + dir * dy };
41 [ + + + + : 378 : if (p.first >= 0 && p.second >= 0 && p.first < cols && p.second < rows) {
+ + + + ]
42 : 237 : antinodes.insert(p);
43 : : }
44 : : }
45 : : }
46 : : }
47 : : }
48 : :
49 : 1 : cout << antinodes.size() << endl;
50 [ + + ]: 3 : }
|