Files
contests/advent_of_code/d9/main.cpp

73 lines
1.5 KiB
C++

#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;
}