From b9cb43378c60c0dbda35df0a9f5bfda06e17815a Mon Sep 17 00:00:00 2001 From: David Vereb Date: Sat, 17 Dec 2022 12:40:42 -0500 Subject: [PATCH] Finished day 8. --- 2022/8/main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/2022/8/main.cpp b/2022/8/main.cpp index 1e35269..ddf30b7 100644 --- a/2022/8/main.cpp +++ b/2022/8/main.cpp @@ -4,6 +4,47 @@ #include #include +long ScenicScore(int set_x, int set_y, std::vector> &trees) +{ + const int tree_val = trees[set_y][set_x]; + + int score_up = 0; + int score_down = 0; + int score_left = 0; + int score_right = 0; + + // up + for(int y = set_y - 1; y >= 0; --y) + { + ++score_up; + if(trees[y][set_x] >= tree_val) + break; + } + // down + for(int y = set_y + 1; y < trees.size(); ++y) + { + ++score_down; + if(trees[y][set_x] >= tree_val) + break; + } + // left + for(int x = set_x - 1; x >= 0; --x) + { + ++score_left; + if(trees[set_y][x] >= tree_val) + break; + } + // right + for(int x = set_x + 1; x < trees[set_y].size(); ++x) + { + ++score_right; + if(trees[set_y][x] >= tree_val) + break; + } + + return score_up * score_down * score_left * score_right; +} + int main() { std::ifstream ifs("data.txt"); @@ -127,16 +168,32 @@ int main() for(auto x = 0; x < trees[y].size(); ++x) { if(visible_coordinates.contains({x,y})) - std::cout << "\033[1;31m"; + std::cout << "\033[1;31m"; // red else - std::cout << "\033[0m"; + std::cout << "\033[0m"; // default white std::cout << trees[y][x]; } std::cout << std::endl; } - std::cout << "\033[0m"; + std::cout << "\033[0m"; // default white std::cout << " Total: " << total << std::endl; + + long highest_scenic = 0; + for(auto y = 0; y < trees.size(); ++y) + { + for(auto x = 0; x < trees.size(); ++x) + { + auto score = ScenicScore(x, y, trees); + if(score > highest_scenic) + { + highest_scenic = score; + std::cout << "New highest: " << score << " at " << x << "," << y << std::endl; + } + } + } + total_pt2 = highest_scenic; + std::cout << "PT2 Total: " << total_pt2 << std::endl; return 0;