Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void follow_water_flow(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map);
- void fill_reservoir(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map)
- {
- std::size_t XSIZE = underground_map.front().size();
- auto[y, x] = faucet_pos;
- std::pair<std::size_t, std::size_t> faucet_left, faucet_right;
- bool found_faucet_left = false, found_faucet_right = false;
- while (!found_faucet_left && !found_faucet_right) {
- underground_map[y][x] = '~';
- // fill to the left
- auto x_left = x;
- while (!found_faucet_left && underground_map[y][x_left] != '#') {
- if (underground_map[y + 1][x_left] == '.' || underground_map[y + 1][x_left] == '|') {
- found_faucet_left = true;
- faucet_left = { y, x_left };
- }
- underground_map[y][x_left] = '~';
- --x_left;
- if (x == 0)
- std::cout << "OOPS" << std::endl;
- }
- auto x_right = x;
- while (!found_faucet_right && underground_map[y][x_right] != '#') {
- if (underground_map[y + 1][x_right] == '.' || underground_map[y + 1][x_right] == '|') {
- found_faucet_right = true;
- faucet_right = { y, x_right };
- }
- underground_map[y][x_right] = '~';
- ++x_right;
- }
- if (found_faucet_left || found_faucet_right) {
- for (std::size_t x = x_left + 1; x <= x_right - 1; ++x) {
- underground_map[y][x] = '|';
- }
- }
- --y;
- }
- if (found_faucet_left)
- follow_water_flow(faucet_left, underground_map);
- if (found_faucet_right)
- follow_water_flow(faucet_right, underground_map);
- }
- void follow_water_flow(std::pair<std::size_t, std::size_t> faucet_pos, std::vector<std::string>& underground_map)
- {
- std::size_t YSIZE = underground_map.size();
- auto [y, x] = faucet_pos;
- //std::cout << "FILL RESERVOIR" << y << " " << x << std::endl;
- do {
- underground_map[y][x] = '|';
- ++y;
- } while (y < YSIZE && underground_map[y][x] != '#' && underground_map[y][x] != '|');
- if (y == YSIZE || underground_map[y][x] == '|')
- return;
- --y;
- fill_reservoir({y, x}, underground_map);
- }
- std::pair<std::size_t, std::size_t> solve_part_one(std::ifstream& ifs)
- {
- std::size_t ymax = 0, xmax = 0;
- int ymin = std::numeric_limits<int>::max();
- std::vector<std::string> underground_map;
- for (std::string line; std::getline(ifs, line);) {
- char a1 = 0, a2 = 0;
- int i1 = 0, i2 = 0, i3 = 0;
- int n = sscanf_s(line.data(), "%c=%d, %c=%d..%d", &a1, 1, &i1, &a2, 1, &i2, &i3);
- if (n != 5) {
- std::cout << "ERROR" << std::endl;
- break;
- }
- if (a1 == 'x') {
- if (i3 + 1 > ymax) {
- ymax = i3 + 1;
- underground_map.resize(ymax,std::string(xmax + 1, '.'));
- }
- ymin = std::min(i2, ymin);
- if (i1 + 1 > xmax) {
- xmax = i1 + 1;
- for (auto& s : underground_map)
- s.resize(xmax + 1, '.');
- }
- for (std::size_t y = i2; y <= i3; ++y) {
- underground_map[y][i1] = '#';
- }
- }
- else {
- if (i1 + 1 > ymax) {
- ymax = i1 + 1;
- underground_map.resize(ymax, std::string(xmax + 1, '.'));
- }
- ymin = std::min(i1, ymin);
- if (i3 + 1 > xmax) {
- xmax = i3 + 1;
- for (auto& s : underground_map)
- s.resize(xmax + 1, '.');
- }
- for (std::size_t x = i2; x <= i3; ++x) {
- underground_map[i1][x] = '#';
- }
- }
- }
- underground_map[0][500] = '+';
- follow_water_flow({ymin, 500}, underground_map);
- std::size_t water_count = 0;
- std::size_t stream_count = 0;
- for (const auto& line : underground_map) {
- water_count += std::count(line.begin(), line.end(), '~');
- stream_count += std::count(line.begin(), line.end(), '|');
- }
- return { water_count + stream_count, water_count };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement