Guest User

aoc 2025 p8

a guest
Dec 8th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7. #include <queue>
  8. #include <cmath>
  9. using namespace std;
  10.  
  11. #define ll long long
  12.  
  13. std::vector<string> split(std::string& s, std::string&& delim) {
  14. std::vector<string> tokens;
  15. int start = 0;
  16. int end;
  17. while( (end = s.find(delim, start)) != string::npos) {
  18. tokens.push_back(s.substr(start, end - start));
  19. start = end + delim.length();
  20. }
  21. if(start < s.length()) {
  22. tokens.push_back(s.substr(start));
  23. }
  24. return tokens;
  25. }
  26.  
  27. struct Coord {
  28. ll x;
  29. ll y;
  30. ll z;
  31. int index;
  32. bool operator==(const Coord& other) const {
  33. return x == other.x && y == other.y && z == other.z;
  34. }
  35. };
  36.  
  37. struct CoordPair {
  38. Coord c1;
  39. Coord c2;
  40. ll dist;
  41. bool operator<(const CoordPair& other) const {
  42. return dist < other.dist;
  43. }
  44. };
  45.  
  46. ll getDist(Coord c1, Coord c2) {
  47. return (std::pow((c1.x - c2.x),2) + std::pow((c1.y - c2.y),2) + std::pow((c1.z - c2.z),2));
  48. }
  49.  
  50. int find(int i, std::vector<int>& parent) {
  51. if(i != parent[i]) {
  52. parent[i] = find(parent[i], parent);
  53. }
  54. return parent[i];
  55. }
  56. void uni(Coord c1, Coord c2, std::vector<int>& parent, std::vector<int>& groupSize, int& groups) {
  57. int p1 = find(c1.index, parent);
  58. int p2 = find(c2.index, parent);
  59. if(p1 == p2) {
  60. return;
  61. }
  62. groups -= 1;
  63. if(groupSize[p1] > groupSize[p2]) {
  64. parent[p2] = p1;
  65. groupSize[p1] += groupSize[p2];
  66. groupSize[p2] = 0;
  67. }
  68. else {
  69. parent[p1] = p2;
  70. groupSize[p2] += groupSize[p1];
  71. groupSize[p1] = 0;
  72. }
  73. }
  74.  
  75. ll solve(std::vector<Coord> coords) {
  76. std::vector<CoordPair> pairs;
  77. for(int i=0; i<coords.size(); i++) {
  78. for(int j = i+1; j<coords.size(); j++) {
  79. pairs.emplace_back(coords[i], coords[j], getDist(coords[i], coords[j]));
  80. }
  81. }
  82. std::sort(pairs.begin(), pairs.end());
  83. std::vector<int> parent(coords.size(), 0);
  84. for(int i=0; i<parent.size(); i++) {
  85. parent[i] = i;
  86. }
  87. std::vector<int> groupSize(coords.size(), 1);
  88. int group = coords.size();
  89. for(int i=0; i<1000; i++) {
  90. const auto& pair = pairs[i];
  91. uni(pair.c1, pair.c2, parent, groupSize, group);
  92. }
  93. std::sort(groupSize.begin(), groupSize.end(), std::greater<int>());
  94. ll ans = groupSize[0] * groupSize[1] * groupSize[2];
  95.  
  96. return ans;
  97. }
  98.  
  99. ll solve2(std::vector<Coord> coords) {
  100. ll ans = 0;
  101. std::vector<CoordPair> pairs;
  102. for(int i=0; i<coords.size(); i++) {
  103. for(int j = i+1; j<coords.size(); j++) {
  104. pairs.emplace_back(coords[i], coords[j], getDist(coords[i], coords[j]));
  105. }
  106. }
  107. std::sort(pairs.begin(), pairs.end());
  108. std::vector<int> parent(coords.size(), 0);
  109. for(int i=0; i<parent.size(); i++) {
  110. parent[i] = i;
  111. }
  112. std::vector<int> groupSize(coords.size(), 1);
  113. int groups = coords.size();
  114. for(int i=0; i<pairs.size(); i++) {
  115. const auto& pair = pairs[i];
  116. uni(pair.c1, pair.c2, parent, groupSize, groups);
  117. if(groups == 1) {
  118. ans = pair.c1.x * pair.c2.x;
  119. break;
  120. }
  121. }
  122.  
  123. return ans;
  124. }
  125.  
  126. int main() {
  127. ifstream inputFile("../data/day8.txt");
  128. std::string line;
  129. std::vector<Coord> coords;
  130. int index = 0;
  131. while(std::getline(inputFile, line)) {
  132. vector<string> coordStrs = split(line, ",");
  133. coords.emplace_back(std::stoull(coordStrs[0]), std::stoull(coordStrs[1]), std::stoull(coordStrs[2]), index++);
  134. }
  135. std::cout << solve2(coords) << std::endl;
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment