Finished aoc day 8
This commit is contained in:
@@ -2,6 +2,51 @@
|
||||
|
||||
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;
|
||||
@@ -16,22 +61,22 @@ int main(){
|
||||
tree_height_map.push_back(acc);
|
||||
}
|
||||
|
||||
size_t WIDTH = tree_height_map.at(0).size();
|
||||
size_t HEIGHT = tree_height_map.size();
|
||||
WIDTH = tree_height_map.at(0).size();
|
||||
HEIGHT = tree_height_map.size();
|
||||
|
||||
vector<vector<bool>> spotted_trees;
|
||||
vector<bool> dummy(WIDTH, false);
|
||||
for(size_t i = 0; i < HEIGHT; ++i){
|
||||
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(size_t i = 0; i < HEIGHT; ++i){
|
||||
for(int i = 0; i < HEIGHT; ++i){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t j = 0; j < WIDTH; ++j){
|
||||
for(int j = 0; j < WIDTH; ++j){
|
||||
if(tree_height_map[i][j] > highest_tree){
|
||||
highest_tree = tree_height_map[i][j];
|
||||
|
||||
@@ -44,10 +89,10 @@ int main(){
|
||||
}
|
||||
|
||||
// From right to left
|
||||
for(size_t i = 0; i < HEIGHT; ++i){
|
||||
for(int i = 0; i < HEIGHT; ++i){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t j = WIDTH; j > 0; --j){
|
||||
for(int j = WIDTH; j > 0; --j){
|
||||
if(tree_height_map[i][j-1] > highest_tree){
|
||||
highest_tree = tree_height_map[i][j-1];
|
||||
|
||||
@@ -60,10 +105,10 @@ int main(){
|
||||
}
|
||||
|
||||
// From top to bottom
|
||||
for(size_t j = 0; j < WIDTH; ++j){
|
||||
for(int j = 0; j < WIDTH; ++j){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t i = 0; i < HEIGHT; ++i){
|
||||
for(int i = 0; i < HEIGHT; ++i){
|
||||
if(tree_height_map[i][j] > highest_tree){
|
||||
highest_tree = tree_height_map[i][j];
|
||||
|
||||
@@ -76,10 +121,10 @@ int main(){
|
||||
}
|
||||
|
||||
// From bottom to top
|
||||
for(size_t j = 0; j < WIDTH; ++j){
|
||||
for(int j = 0; j < WIDTH; ++j){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t i = HEIGHT; i > 0; --i){
|
||||
for(int i = HEIGHT; i > 0; --i){
|
||||
if(tree_height_map[i-1][j] > highest_tree){
|
||||
highest_tree = tree_height_map[i-1][j];
|
||||
|
||||
@@ -93,5 +138,18 @@ int main(){
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user