diff --git a/2022/8/main.cpp b/2022/8/main.cpp index 1e35269..19e258e 100644 --- a/2022/8/main.cpp +++ b/2022/8/main.cpp @@ -4,6 +4,54 @@ #include #include +std::vector ParseRow(auto begin, auto end) +{ + std::vector rtn_indexes; + std::vector values; + + // initialize list with the first value + values.push_back(*begin); + + // iterate through the rest: + for(auto i = begin + 1; i != end; ++i) + { + + // if it's higher than the most recent one: + const auto val = *i; + if(val > values.back()) + { + values.push_back(val); + rtn_indexes.push_back(std::distance(begin, i)); + } + } + + return rtn_indexes; +} + +std::vector ParseCol(auto begin, auto end, int col) +{ + std::vector rtn_indexes; + std::vector values; + + // initialize list with the first value + values.push_back((*begin)[col]); + + // iterate through the rest: + for(auto i = begin + 1; i != end; ++i) + { + + // if it's higher than the most recent one: + const auto val = (*i)[col]; + if(val > values.back()) + { + values.push_back(val); + rtn_indexes.push_back(std::distance(begin, i)); + } + } + + return rtn_indexes; +} + int main() { std::ifstream ifs("data.txt"); @@ -45,79 +93,27 @@ int main() std::vector points; for(auto y = 1; y < trees.size() - 1; ++y) { - points.clear(); + // left to right: + points = ParseRow(trees[y].begin(), trees[y].end()); + for(const auto &point : points) + visible_coordinates.insert({point,y}); - // 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}); - } - } + // right to left: + points = ParseRow(trees[y].rbegin(), trees[y].rend()); + for(const auto &point : points) + visible_coordinates.insert({trees[y].size() - point - 1,y}); } - for(auto y = 1; y < trees.size() - 1; ++y) + for(auto x = 1; x < trees.size() - 1; ++x) { - points.clear(); + // left to right: + points = ParseCol(trees.begin(), trees.end(), x); + for(const auto &point : points) + visible_coordinates.insert({x,point}); - // 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}); - } - } + // right to left: + points = ParseCol(trees.rbegin(), trees.rend(), x); + for(const auto &point : points) + visible_coordinates.insert({x, trees.size() - point - 1}); } total += visible_coordinates.size();