155 lines
3.7 KiB
C++
155 lines
3.7 KiB
C++
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
int WIDTH, HEIGHT;
|
|
|
|
int scenic_score(int i, int j, const vector<vector<int>>& tree_height_map){
|
|
int left = 0, right = 0, up = 0, down = 0;
|
|
int tree_height = tree_height_map[i][j];
|
|
|
|
// Look to the right
|
|
for(int x = j + 1; x < WIDTH; ++x){
|
|
if(tree_height_map[i][x] >= tree_height){
|
|
right++;
|
|
break;
|
|
}
|
|
right++;
|
|
}
|
|
|
|
// Look to the left
|
|
for(int x = j - 1; x >= 0; --x){
|
|
if(tree_height_map[i][x] >= tree_height){
|
|
left++;
|
|
break;
|
|
}
|
|
left++;
|
|
}
|
|
|
|
// Look down
|
|
for(int y = i + 1; y < HEIGHT; ++y){
|
|
if(tree_height_map[y][j] >= tree_height){
|
|
down++;
|
|
break;
|
|
}
|
|
down++;
|
|
}
|
|
|
|
// Look up
|
|
for(int y = i - 1; y >= 0; --y){
|
|
if(tree_height_map[y][j] >= tree_height){
|
|
up++;
|
|
break;
|
|
}
|
|
up++;
|
|
}
|
|
|
|
return left * right * up * down;
|
|
}
|
|
|
|
int main(){
|
|
ifstream input_file("input.txt");
|
|
string line;
|
|
|
|
vector<vector<int>> tree_height_map;
|
|
|
|
while(getline(input_file, line)){
|
|
vector<int> acc;
|
|
for(char c : line){
|
|
acc.push_back(c - '0');
|
|
}
|
|
tree_height_map.push_back(acc);
|
|
}
|
|
|
|
WIDTH = tree_height_map.at(0).size();
|
|
HEIGHT = tree_height_map.size();
|
|
|
|
vector<vector<bool>> spotted_trees;
|
|
vector<bool> dummy(WIDTH, false);
|
|
for(int i = 0; i < HEIGHT; ++i){
|
|
spotted_trees.push_back(dummy);
|
|
}
|
|
|
|
int result = 0;
|
|
// Now check from all directions
|
|
// Left to right first
|
|
for(int i = 0; i < HEIGHT; ++i){
|
|
int highest_tree = -1;
|
|
|
|
for(int j = 0; j < WIDTH; ++j){
|
|
if(tree_height_map[i][j] > highest_tree){
|
|
highest_tree = tree_height_map[i][j];
|
|
|
|
if(!spotted_trees[i][j]){
|
|
result++;
|
|
spotted_trees[i][j] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// From right to left
|
|
for(int i = 0; i < HEIGHT; ++i){
|
|
int highest_tree = -1;
|
|
|
|
for(int j = WIDTH; j > 0; --j){
|
|
if(tree_height_map[i][j-1] > highest_tree){
|
|
highest_tree = tree_height_map[i][j-1];
|
|
|
|
if(!spotted_trees[i][j-1]){
|
|
result++;
|
|
spotted_trees[i][j-1] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// From top to bottom
|
|
for(int j = 0; j < WIDTH; ++j){
|
|
int highest_tree = -1;
|
|
|
|
for(int i = 0; i < HEIGHT; ++i){
|
|
if(tree_height_map[i][j] > highest_tree){
|
|
highest_tree = tree_height_map[i][j];
|
|
|
|
if(!spotted_trees[i][j]){
|
|
result++;
|
|
spotted_trees[i][j] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// From bottom to top
|
|
for(int j = 0; j < WIDTH; ++j){
|
|
int highest_tree = -1;
|
|
|
|
for(int i = HEIGHT; i > 0; --i){
|
|
if(tree_height_map[i-1][j] > highest_tree){
|
|
highest_tree = tree_height_map[i-1][j];
|
|
|
|
if(!spotted_trees[i-1][j]){
|
|
result++;
|
|
spotted_trees[i-1][j] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
cout << "Number of spotted trees: " << result << endl;
|
|
|
|
// For part 2, let's try checking all hidden trees on their viewing distances
|
|
// and see if we're not too slow with such a brute force
|
|
|
|
int max_scenic_score = 0;
|
|
for(int i = 0; i < HEIGHT; ++i){
|
|
for(int j = 0; j < WIDTH; ++j){
|
|
if(spotted_trees[i][j]){
|
|
max_scenic_score = max(max_scenic_score, scenic_score(i, j, tree_height_map));
|
|
}
|
|
}
|
|
}
|
|
cout << "Maximal scenic score: " << max_scenic_score << endl;
|
|
|
|
return 0;
|
|
} |