-
Notifications
You must be signed in to change notification settings - Fork 0
/
d8-sol.cpp
113 lines (94 loc) · 3.45 KB
/
d8-sol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using namespace std;
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
int locToInt(int r, int c, int m) {
return r * m + c;
}
pair<int, int> intToLoc(int i, int m) {
int c = i % m;
int r = i / m;
return {r, c};
}
int findAntinodes(const vector<vector<char>> &grid, int m, int n, bool partA) {
// Store antinode & antenna locations
unordered_set<int> antinodes;
unordered_map<char, vector<pair<int, int>>> antennas;
// Loop to find antennas
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != '.') antennas[grid[i][j]].emplace_back(i, j);
}
}
// Loop to calculate antinode locations
for (auto it : antennas) {
// cout << "Iterating through " << it.first << "\n";
auto vec = it.second;
int sz = vec.size();
for (int i = 0; i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
// cout << vec[i].first << " " << vec[i].second << "\t" << vec[j].first << " " << vec[j].second << "\t\t";
int rdif = vec[j].first - vec[i].first;
int cdif = vec[j].second - vec[i].second;
// cout << vec[i].first - rdif << " " << vec[i].second - cdif << "\t";
// cout << vec[j].first + rdif << " " << vec[j].second + cdif << "\n";
if (partA) {
if (vec[i].first - rdif >= 0 && vec[i].second - cdif < n && vec[i].second - cdif >= 0) {
antinodes.insert(locToInt(vec[i].first - rdif, vec[i].second - cdif, m));
}
if (vec[j].first + rdif < m && vec[j].second + cdif < n && vec[j].second + cdif >= 0) {
antinodes.insert(locToInt(vec[j].first + rdif, vec[j].second + cdif, m));
}
} else {
int k = vec[i].first;
int l = vec[i].second;
while (k >= 0 && l < n && l >= 0) {
// cout << k << " " << l << "\t";
antinodes.insert(locToInt(k, l, m));
k -= rdif;
l -= cdif;
}
k = vec[j].first;
l = vec[j].second;
while (k < m && l < n && l >= 0) {
// cout << k << " " << l << "\t";
antinodes.insert(locToInt(k, l, m));
k += rdif;
l += cdif;
}
// cout << "\n";
}
}
}
}
return size(antinodes);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Usage: ./d8-sol [input filename]\n";
exit(1);
}
// Declare grid
vector<vector<char>> grid;
// Read in file input
ifstream file(argv[1]);
string str;
int row = 0;
while (file >> str) {
grid.push_back({});
for (auto ch : str) {
grid[row].push_back(ch);
}
row++;
}
// Get grid size
int m = grid.size();
int n = grid[0].size();
int partASolution = findAntinodes(grid, m, n, true);
cout << "Number of antinodes in range: " << partASolution << "\n";
int partBSolution = findAntinodes(grid, m, n, false);
cout << "Number of antinodes in range, accounting for resonant harmonics: " << partBSolution << "\n";
return 0;
}