Finished day 8.

This commit is contained in:
David Vereb 2022-12-17 12:40:42 -05:00
parent 575b9e8aed
commit b9cb43378c

View File

@ -4,6 +4,47 @@
#include <string> #include <string>
#include <vector> #include <vector>
long ScenicScore(int set_x, int set_y, std::vector<std::vector<int>> &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() int main()
{ {
std::ifstream ifs("data.txt"); std::ifstream ifs("data.txt");
@ -127,16 +168,32 @@ int main()
for(auto x = 0; x < trees[y].size(); ++x) for(auto x = 0; x < trees[y].size(); ++x)
{ {
if(visible_coordinates.contains({x,y})) if(visible_coordinates.contains({x,y}))
std::cout << "\033[1;31m"; std::cout << "\033[1;31m"; // red
else else
std::cout << "\033[0m"; std::cout << "\033[0m"; // default white
std::cout << trees[y][x]; std::cout << trees[y][x];
} }
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << "\033[0m"; std::cout << "\033[0m"; // default white
std::cout << " Total: " << total << std::endl; 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; std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0; return 0;