diff --git a/2022/8/main.cpp b/2022/8/main.cpp index 930b068..1e35269 100644 --- a/2022/8/main.cpp +++ b/2022/8/main.cpp @@ -29,132 +29,112 @@ int main() trees.back().push_back(ch - '0'); } - total += 2 * trees.size(); - total += 2 * (trees[0].size() - 2); - std::set> visible_coordinates; - - // NOTE(dev): -#warning This shit is wrong because it's only finding the FIRST visible tree, not all visible trees. -#warning e.g. If you have 2003005003007004005 -#warning ^ ^ ^ ^ are all visible from the left -#warning ^ is visible from the right - - // left to right, right to left IN EACH ROW: - for(auto y = 1; y < trees.size() - 1; ++y) + for(auto y = 0; y < trees.size(); ++y) { - auto x = 1; - bool found_from_left = false; - while(x < trees[y].size() - 1) - { - std::cout << trees[y][x]; - if(trees[y][x] < trees[y][x-1]) - break; - found_from_left = true; - ++x; - } - if(found_from_left) - { - // make sure it isn't the right-most, as that's already counted - if(x == trees[y].size() - 1) - { - if(trees[y][x] < trees[y][x-1]) - { - std::cout << ": insert {" << (x - 1) << "," << y << "}" - << "(" << trees[y][x-1] << ")"; - visible_coordinates.insert({x - 1, y}); - } - } - else - { - std::cout << ": insert {" << (x - 1) << "," << y << "}" - << "(" << trees[y][x-1] << ")"; - visible_coordinates.insert({x - 1, y}); - - // NOTE(dev): Only search from the right if there's still room to search: - bool found_from_right = false; - auto found_x = x - 1; - x = trees[y].size() - 2; - while(x > found_x) - { - if(trees[y][x] < trees[y][x + 1]) - break; - found_from_right = true; - --x; - } - if(found_from_right) - { - std::cout << ": insert {" << (x + 1) << "," << y << "}" - << "(" << trees[y][x+1] << ")"; - visible_coordinates.insert({x + 1, y}); - } - } - } - std::cout << std::endl; + visible_coordinates.insert({0,y}); + visible_coordinates.insert({trees[y].size()-1,y}); } - - // top to bottom, bottom to top IN EACH COLUMN: for(auto x = 1; x < trees[0].size() - 1; ++x) { - auto y = 1; - bool found_from_top = false; - while(y < trees.size() - 1) - { - if(trees[y][x] < trees[y-1][x]) - break; - found_from_top = true; - ++x; - } - if(found_from_top) - { - // make sure it isn't the bottom-most, as that's already counted - if(y == trees.size() - 1) - { - if(trees[y][x] < trees[y-1][x]) - { - std::cout << ": insert {" << x << "," << (y - 1) << "}" - << "(" << trees[y-1][x] << ")"; - visible_coordinates.insert({x, y - 1}); - } - } - else - { - std::cout << ": insert {" << x << "," << (y - 1) << "}" - << "(" << trees[y-1][x] << ")"; - visible_coordinates.insert({x, y - 1}); + visible_coordinates.insert({x,0}); + visible_coordinates.insert({x,trees.size()-1}); + } - // NOTE(dev): Only search from the bottom if there's still room to search: - bool found_from_bottom = false; - auto found_y = y - 1; - y = trees.size() - 2; - while(y > found_y) - { - if(trees[y][x] < trees[y + 1][x]) - break; - found_from_bottom = true; - --y; - } - if(found_from_bottom) - { - std::cout << ": insert {" << x << "," << (y + 1) << "}" - << "(" << trees[y+1][x] << ")"; - visible_coordinates.insert({x, y + 1}); - } + // left to right, right to left IN EACH ROW: + std::vector points; + for(auto y = 1; y < trees.size() - 1; ++y) + { + points.clear(); + + // initialize list with the first value + points.push_back(trees[y][0]); + + // iterate through the rest: + for(auto x = 1; x < trees[y].size(); ++x) + { + // if it's higher than the most recent one: + const auto val = trees[y][x]; + if(val > points.back()) + { + points.push_back(val); + visible_coordinates.insert({x,y}); + } + } + } + for(auto y = 1; y < trees.size() - 1; ++y) + { + points.clear(); + + // initialize list with the first value + points.push_back(trees[y][trees.size()-1]); + + // iterate through the rest: + for(auto x = trees[y].size() - 2; x > 0; --x) + { + // if it's higher than the most recent one: + const auto val = trees[y][x]; + if(val > points.back()) + { + points.push_back(val); + visible_coordinates.insert({x,y}); + } + } + } + for(auto x = 1; x < trees[0].size() - 1; ++x) + { + points.clear(); + + // initialize list with the first value + points.push_back(trees[0][x]); + + // iterate through the rest: + for(auto y = 1; y < trees.size(); ++y) + { + // if it's higher than the most recent one: + const auto val = trees[y][x]; + if(val > points.back()) + { + points.push_back(val); + visible_coordinates.insert({x,y}); + } + } + } + for(auto x = 1; x < trees[0].size() - 1; ++x) + { + points.clear(); + + // initialize list with the first value + points.push_back(trees[trees.size()-1][x]); + + // iterate through the rest: + for(auto y = trees.size()-2; y > 0; --y) + { + // if it's higher than the most recent one: + const auto val = trees[y][x]; + if(val > points.back()) + { + points.push_back(val); + visible_coordinates.insert({x,y}); } } } total += visible_coordinates.size(); - // // track interior - // for(auto y = 1; y < trees.size() - 1; ++y) - // { - // // left & right, first - // for(auto x = 1; x < trees[y].size() - 1; ++x) - // { - - // } - // } + for(auto y = 0; y < trees.size(); ++y) + { + for(auto x = 0; x < trees[y].size(); ++x) + { + if(visible_coordinates.contains({x,y})) + std::cout << "\033[1;31m"; + else + std::cout << "\033[0m"; + std::cout << trees[y][x]; + } + std::cout << std::endl; + } + std::cout << "\033[0m"; std::cout << " Total: " << total << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl;