Guest User

Untitled

a guest
Dec 8th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <unordered_set>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. struct hash_pair {
  9. size_t operator()(const pair<int,int>& p) const
  10. {
  11. return (p.first << 16) | p.second;
  12. }
  13. };
  14.  
  15. bool inBounds(int x, int y, int maxR, int maxC) {
  16. if (x >=0 && y >= 0 && x < maxR && y < maxC) return true;
  17. return false;
  18. }
  19.  
  20. void findAntinodesForType(const vector<pair<int,int>>& pos, unordered_set<pair<int,int>, hash_pair>& visited, int maxR, int maxC, bool part1) {
  21. // look for every pair in the vector & determine antinodes
  22. for (int i=0; i<pos.size(); ++i) {
  23. for (int j=i+1; j<pos.size(); ++j) {
  24.  
  25. // dist between pair
  26. double x_dist = pos[i].first - pos[j].first;
  27. double y_dist = pos[i].second - pos[j].second;
  28.  
  29. // extend from that line to find antinodes
  30. int mult = part1 ? 1 : 0; // only include the current antennae if in part2
  31.  
  32. while (inBounds(pos[i].first + mult * x_dist, pos[i].second + mult * y_dist, maxR, maxC)) { // consider the entire line in this dir, until off the grid
  33. visited.insert({pos[i].first + mult * x_dist, pos[i].second + mult * y_dist});
  34. ++mult;
  35. if (part1) break;
  36. }
  37.  
  38. mult = part1 ? 1 : 0; // only include the paired anteannae if in part2
  39.  
  40. while (inBounds(pos[j].first - mult * x_dist, pos[j].second - mult * y_dist, maxR, maxC)) {
  41. visited.insert({pos[j].first - mult * x_dist, pos[j].second - mult * y_dist});
  42. ++mult;
  43. if (part1) break;
  44. }
  45.  
  46. }
  47. }
  48. }
  49.  
  50. unordered_map<char, vector<pair<int,int>>> parse(int& maxR, int& maxC) {
  51. unordered_map<char, vector<pair<int,int>>> allCoords;
  52. string s;
  53.  
  54. int r = -1; int c = -1;
  55.  
  56. while (getline(cin, s)) {
  57. ++r;
  58. maxR = max(r+1, maxR);
  59. stringstream ss(s);
  60. char ch;
  61. while (ss >> ch) {
  62. c++;
  63. maxC = max(c+1, maxC);
  64. if (ch == '.') continue;
  65. pair<int,int> curr {r,c};
  66.  
  67. if (auto iter = allCoords.find(ch); iter != allCoords.end())
  68. iter->second.emplace_back(curr);
  69. else
  70. allCoords[ch] = {curr};
  71. }
  72. c = -1;
  73. }
  74. return allCoords;
  75. }
  76.  
  77. int part(unordered_map<char, vector<pair<int,int>>>& allCoords, int maxR, int maxC, bool part1) {
  78. unordered_set<pair<int,int>, hash_pair> visited;
  79.  
  80. for (auto coords : allCoords) {
  81. findAntinodesForType(coords.second, visited, maxR, maxC, part1);
  82. }
  83. return visited.size();
  84. }
  85.  
  86. int main() {
  87. int maxR = -1; int maxC = -1;
  88. unordered_map<char, vector<pair<int,int>>> allCoords = parse(maxR, maxC);
  89.  
  90. cout << "part1: " << part(allCoords, maxR, maxC, true) << endl;
  91. cout << "part2: " << part(allCoords, maxR, maxC, false) << endl;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment