Finished day 8 aoc, part 1
This commit is contained in:
97
advent_of_code/2022/8/main.cpp
Normal file
97
advent_of_code/2022/8/main.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
size_t WIDTH = tree_height_map.at(0).size();
|
||||
size_t HEIGHT = tree_height_map.size();
|
||||
|
||||
vector<vector<bool>> spotted_trees;
|
||||
vector<bool> dummy(WIDTH, false);
|
||||
for(size_t 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){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t 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(size_t i = 0; i < HEIGHT; ++i){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t 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(size_t j = 0; j < WIDTH; ++j){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t 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(size_t j = 0; j < WIDTH; ++j){
|
||||
int highest_tree = -1;
|
||||
|
||||
for(size_t 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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user