Advertisement
Guest User

Untitled

a guest
Dec 10th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <queue>
  4. #include <set>
  5. #include <map>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. vector<pair<int,int>> delta = {{0,1}, {0,-1}, {1,0}, {-1,0}};
  10.  
  11. int doBFS(int startR, int startC, const vector<vector<int>>& grid) {
  12. int count = 0;
  13.  
  14. set<pair<int,int>> visited;
  15. queue<pair<int,int>> bfs;
  16.  
  17. bfs.push({startR, startC});
  18. visited.insert({startR, startC});
  19.  
  20. while (!bfs.empty()) {
  21. auto top = bfs.front();
  22. bfs.pop();
  23.  
  24. if (grid[top.first][top.second] == 9) {
  25. // cout << "got to 9 at: " << top.first << " , " << top.second << endl;
  26. ++count;
  27. continue;
  28. }
  29.  
  30. // add neighbours
  31. for (const auto & d : delta) {
  32. int nextR = top.first + d.first;
  33. int nextC = top.second + d.second;
  34.  
  35. if (nextR >= 0 && nextC >= 0 && nextR < grid.size() && nextC < grid[0].size()
  36. && visited.find({nextR, nextC}) == visited.end() && grid[nextR][nextC] == grid[top.first][top.second]+1) {
  37. visited.insert({nextR, nextC});
  38. bfs.push({nextR, nextC});
  39. }
  40. }
  41. }
  42. return count;
  43. }
  44.  
  45. int doBFSCountAllPaths(int startR, int startC, const vector<vector<int>>& grid) {
  46. int count = 0;
  47.  
  48. map<pair<int,int>, int> visitedCount;
  49. queue<pair<int,int>> bfs;
  50.  
  51. bfs.push({startR, startC});
  52. visitedCount[{startR, startC}] = 1;
  53.  
  54. while (!bfs.empty()) {
  55. auto top = bfs.front();
  56. bfs.pop();
  57.  
  58. // cout << "visiting: " << top.first << " , " << top.second << " : " << grid[top.first][top.second] << " count: " << visitedCount[top] << endl;
  59.  
  60. if (grid[top.first][top.second] == 9) {
  61. // cout << "got to 9 at: " << top.first << " , " << top.second << endl;
  62. count += visitedCount[top];
  63. continue;
  64. }
  65.  
  66. // add neighbours
  67. for (const auto & d : delta) {
  68. int nextR = top.first + d.first;
  69. int nextC = top.second + d.second;
  70.  
  71. if (nextR >= 0 && nextC >= 0 && nextR < grid.size() && nextC < grid[0].size() && grid[nextR][nextC] == grid[top.first][top.second]+1) {
  72. if (auto it = visitedCount.find({nextR, nextC}); it != visitedCount.end()) {
  73. it->second += visitedCount[top];
  74. } else {
  75. visitedCount[{nextR, nextC}] = visitedCount[top];
  76. bfs.push({nextR, nextC});
  77. }
  78. }
  79. }
  80. }
  81. return count;
  82. }
  83.  
  84. int part1(const vector<vector<int>>& grid) {
  85. int score = 0;
  86. for (int r=0; r<grid.size(); ++r) {
  87. for (int c=0; c<grid[0].size(); ++c) {
  88. // if trailhead, get score
  89. if (grid[r][c] == 0)
  90. score += doBFS(r, c, grid);
  91. }
  92. }
  93. return score;
  94. }
  95.  
  96. int part2(const vector<vector<int>>& grid) {
  97. int score = 0;
  98. for (int r=0; r<grid.size(); ++r) {
  99. for (int c=0; c<grid[0].size(); ++c) {
  100. // if trailhead, get score
  101. if (grid[r][c] == 0)
  102. score += doBFSCountAllPaths(r, c, grid);
  103. }
  104. }
  105. return score;
  106. }
  107.  
  108. vector<vector<int>> parse() {
  109. vector<vector<int>> grid;
  110.  
  111. string row;
  112. while (cin >> row) {
  113. vector<int> rowVec;
  114.  
  115. stringstream ss(row);
  116. char c;
  117. while (ss >> c) rowVec.emplace_back(c - '0');
  118.  
  119. grid.emplace_back(rowVec);
  120. }
  121. return grid;
  122. }
  123.  
  124. int main() {
  125. vector<vector<int>> grid = parse();
  126.  
  127. int p1 = part1(grid);
  128. cout << "part1: " << p1 << endl;
  129.  
  130. int p2 = part2(grid);
  131. cout << "part2: " << p2 << endl;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement