Part 1 day 9 of advent of code finished

This commit is contained in:
2021-12-30 01:25:36 +01:00
parent 43549c9d38
commit d91f9e1266
3 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
#include <bits/stdc++.h>
using namespace std;
string file_name = "9.txt";
// string file_name = "test.txt";
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
vector<vector<int>> heights;
ifstream file_stream;
file_stream.open(file_name);
if(file_stream.is_open()){
string line;
while(getline(file_stream, line)){
vector<int> line_input;
for(const char &c : line){
line_input.push_back(c - '0');
}
heights.push_back(line_input);
}
}
// Test input parsing
// for(auto v : heights){
// for(auto i : v){
// cout << i;
// } cout << endl;
// } cout << endl;
size_t HEIGHT = heights.size() - 1, WIDTH = heights[0].size() - 1;
int result = 0;
for(size_t i = 0; i <= HEIGHT; ++i){
for(size_t j = 0; j <= WIDTH; ++j){
bool lowest = true;
int cur = heights[i][j];
// Check on corner cases (ha literally)
if(i != 0){
lowest &= cur < heights[i - 1][j];
}
if(i != HEIGHT){
lowest &= cur < heights[i + 1][j];
}
if(j != 0){
lowest &= cur < heights[i][j - 1];
}
if(j != WIDTH){
lowest &= cur < heights[i][j + 1];
}
if(lowest){
result += cur + 1;
}
}
}
cout << result << endl;
cout << flush;
return 0;
}