2022-12-08 23:57:49 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-12-17 12:40:42 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-08 23:57:49 -05:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
std::ifstream ifs("data.txt");
|
|
|
|
if(!ifs.is_open())
|
|
|
|
{
|
|
|
|
std::cerr << "Missing data.txt." << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long total = 0;
|
|
|
|
unsigned long total_pt2 = 0;
|
|
|
|
|
|
|
|
std::vector<std::vector<int>> trees;
|
|
|
|
|
|
|
|
// Parse:
|
|
|
|
for(std::string line; std::getline(ifs, line); )
|
|
|
|
{
|
|
|
|
if(line == "")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
trees.push_back({});
|
|
|
|
for(const char &ch : line)
|
|
|
|
trees.back().push_back(ch - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::pair<int, int>> visible_coordinates;
|
2022-12-11 16:59:56 -05:00
|
|
|
for(auto y = 0; y < trees.size(); ++y)
|
|
|
|
{
|
|
|
|
visible_coordinates.insert({0,y});
|
|
|
|
visible_coordinates.insert({trees[y].size()-1,y});
|
|
|
|
}
|
|
|
|
for(auto x = 1; x < trees[0].size() - 1; ++x)
|
|
|
|
{
|
|
|
|
visible_coordinates.insert({x,0});
|
|
|
|
visible_coordinates.insert({x,trees.size()-1});
|
|
|
|
}
|
2022-12-08 23:57:49 -05:00
|
|
|
|
|
|
|
// left to right, right to left IN EACH ROW:
|
2022-12-11 16:59:56 -05:00
|
|
|
std::vector<int> points;
|
2022-12-08 23:57:49 -05:00
|
|
|
for(auto y = 1; y < trees.size() - 1; ++y)
|
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
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)
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
// if it's higher than the most recent one:
|
|
|
|
const auto val = trees[y][x];
|
|
|
|
if(val > points.back())
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
points.push_back(val);
|
|
|
|
visible_coordinates.insert({x,y});
|
2022-12-08 23:57:49 -05:00
|
|
|
}
|
2022-12-11 16:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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())
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
points.push_back(val);
|
|
|
|
visible_coordinates.insert({x,y});
|
2022-12-08 23:57:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto x = 1; x < trees[0].size() - 1; ++x)
|
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
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)
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
// if it's higher than the most recent one:
|
|
|
|
const auto val = trees[y][x];
|
|
|
|
if(val > points.back())
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
points.push_back(val);
|
|
|
|
visible_coordinates.insert({x,y});
|
2022-12-08 23:57:49 -05:00
|
|
|
}
|
2022-12-11 16:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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())
|
2022-12-08 23:57:49 -05:00
|
|
|
{
|
2022-12-11 16:59:56 -05:00
|
|
|
points.push_back(val);
|
|
|
|
visible_coordinates.insert({x,y});
|
2022-12-08 23:57:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
total += visible_coordinates.size();
|
|
|
|
|
2022-12-11 16:59:56 -05:00
|
|
|
for(auto y = 0; y < trees.size(); ++y)
|
|
|
|
{
|
|
|
|
for(auto x = 0; x < trees[y].size(); ++x)
|
|
|
|
{
|
|
|
|
if(visible_coordinates.contains({x,y}))
|
2022-12-17 12:40:42 -05:00
|
|
|
std::cout << "\033[1;31m"; // red
|
2022-12-11 16:59:56 -05:00
|
|
|
else
|
2022-12-17 12:40:42 -05:00
|
|
|
std::cout << "\033[0m"; // default white
|
2022-12-11 16:59:56 -05:00
|
|
|
std::cout << trees[y][x];
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2022-12-17 12:40:42 -05:00
|
|
|
std::cout << "\033[0m"; // default white
|
2022-12-08 23:57:49 -05:00
|
|
|
|
|
|
|
std::cout << " Total: " << total << std::endl;
|
2022-12-17 12:40:42 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-12-08 23:57:49 -05:00
|
|
|
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|