Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. void follow_water_flow(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map);
  2.  
  3. void fill_reservoir(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map)
  4. {
  5. std::size_t XSIZE = underground_map.front().size();
  6.  
  7. auto[y, x] = faucet_pos;
  8. std::pair<std::size_t, std::size_t> faucet_left, faucet_right;
  9. bool found_faucet_left = false, found_faucet_right = false;
  10. while (!found_faucet_left && !found_faucet_right) {
  11. underground_map[y][x] = '~';
  12.  
  13. // fill to the left
  14. auto x_left = x;
  15. while (!found_faucet_left && underground_map[y][x_left] != '#') {
  16. if (underground_map[y + 1][x_left] == '.' || underground_map[y + 1][x_left] == '|') {
  17. found_faucet_left = true;
  18. faucet_left = { y, x_left };
  19. }
  20.  
  21. underground_map[y][x_left] = '~';
  22. --x_left;
  23.  
  24. if (x == 0)
  25. std::cout << "OOPS" << std::endl;
  26. }
  27.  
  28. auto x_right = x;
  29. while (!found_faucet_right && underground_map[y][x_right] != '#') {
  30. if (underground_map[y + 1][x_right] == '.' || underground_map[y + 1][x_right] == '|') {
  31. found_faucet_right = true;
  32. faucet_right = { y, x_right };
  33. }
  34.  
  35. underground_map[y][x_right] = '~';
  36. ++x_right;
  37. }
  38.  
  39. if (found_faucet_left || found_faucet_right) {
  40. for (std::size_t x = x_left + 1; x <= x_right - 1; ++x) {
  41. underground_map[y][x] = '|';
  42. }
  43. }
  44.  
  45. --y;
  46. }
  47.  
  48. if (found_faucet_left)
  49. follow_water_flow(faucet_left, underground_map);
  50. if (found_faucet_right)
  51. follow_water_flow(faucet_right, underground_map);
  52. }
  53.  
  54. void follow_water_flow(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map)
  55. {
  56. std::size_t YSIZE = underground_map.size();
  57.  
  58. auto [y, x] = faucet_pos;
  59. //std::cout << "FILL RESERVOIR" << y << " " << x << std::endl;
  60. do {
  61. underground_map[y][x] = '|';
  62. ++y;
  63. } while (y < YSIZE && underground_map[y][x] != '#' && underground_map[y][x] != '|');
  64.  
  65. if (y == YSIZE || underground_map[y][x] == '|')
  66. return;
  67. --y;
  68. fill_reservoir({y, x}, underground_map);
  69. }
  70.  
  71. std::pair<std::size_t, std::size_t> solve_part_one(std::ifstream& ifs)
  72. {
  73. std::size_t ymax = 0, xmax = 0;
  74. int ymin = std::numeric_limits<int>::max();
  75. std::vector<std::string> underground_map;
  76. for (std::string line; std::getline(ifs, line);) {
  77. char a1 = 0, a2 = 0;
  78. int i1 = 0, i2 = 0, i3 = 0;
  79. int n = sscanf_s(line.data(), "%c=%d, %c=%d..%d", &a1, 1, &i1, &a2, 1, &i2, &i3);
  80. if (n != 5) {
  81. std::cout << "ERROR" << std::endl;
  82. break;
  83. }
  84.  
  85. if (a1 == 'x') {
  86. if (i3 + 1 > ymax) {
  87. ymax = i3 + 1;
  88. underground_map.resize(ymax,std::string(xmax + 1, '.'));
  89. }
  90.  
  91. ymin = std::min(i2, ymin);
  92.  
  93. if (i1 + 1 > xmax) {
  94. xmax = i1 + 1;
  95. for (auto& s : underground_map)
  96. s.resize(xmax + 1, '.');
  97. }
  98.  
  99. for (std::size_t y = i2; y <= i3; ++y) {
  100. underground_map[y][i1] = '#';
  101. }
  102. }
  103. else {
  104. if (i1 + 1 > ymax) {
  105. ymax = i1 + 1;
  106. underground_map.resize(ymax, std::string(xmax + 1, '.'));
  107. }
  108.  
  109. ymin = std::min(i1, ymin);
  110.  
  111.  
  112. if (i3 + 1 > xmax) {
  113. xmax = i3 + 1;
  114. for (auto& s : underground_map)
  115. s.resize(xmax + 1, '.');
  116. }
  117.  
  118. for (std::size_t x = i2; x <= i3; ++x) {
  119. underground_map[i1][x] = '#';
  120. }
  121. }
  122. }
  123.  
  124. underground_map[0][500] = '+';
  125.  
  126. follow_water_flow({ymin, 500}, underground_map);
  127.  
  128. std::size_t water_count = 0;
  129. std::size_t stream_count = 0;
  130. for (const auto& line : underground_map) {
  131. water_count += std::count(line.begin(), line.end(), '~');
  132. stream_count += std::count(line.begin(), line.end(), '|');
  133. }
  134.  
  135. return { water_count + stream_count, water_count };
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement