-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_09a.cpp
57 lines (52 loc) · 1.36 KB
/
day_09a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
void print(std::vector<std::vector<int>>& v) {
for (const auto& row : v) {
for(const auto ele : row) {
std::cout << ele;
}
std::cout << '\n';
}
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_09_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<int>> h_map;
h_map.emplace_back();
while(std::getline(file, line)) {
h_map.emplace_back();
h_map.back().emplace_back(9);
for(const auto c : line) {
h_map.back().emplace_back(c - '0');
}
h_map.back().emplace_back(9);
}
const std::vector<int> temp (h_map[1].size(), 9);
h_map[0] = temp;
h_map.emplace_back(temp);
const auto lowest = [](const int row, const int col, const std::vector<std::vector<int>>& h_map) {
return (
h_map[row+1][col] > h_map[row][col] &&
h_map[row-1][col] > h_map[row][col] &&
h_map[row][col+1] > h_map[row][col] &&
h_map[row][col-1] > h_map[row][col]
);
};
long long risk = 0;
for (int row = 1; row < h_map.size() - 1; row ++) {
for (int col = 1; col < h_map[row].size() - 1; col++) {
if (lowest(row, col, h_map)) {
risk += 1 + h_map[row][col];
}
}
}
std::cout << risk << '\n';
return 0;
}