-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_08b.cpp
68 lines (57 loc) · 1.61 KB
/
day_08b.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
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char * argv[]) {
std::string input = "../input/day_08_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<int>> heights;
while(std::getline(file, line)) {
heights.emplace_back();
for (const char c : line) {
heights.back().emplace_back(c-'0');
}
}
std::vector<std::vector<int>> scores(
heights.size(),
std::vector<int>(heights[0].size(), 1)
);
const std::vector<std::vector<int>> motions {
{0,1},
{1,0},
{0,-1},
{-1,0}
};
const int max_rows = heights.size();
const int max_cols = heights[0].size();
const auto in_bounds = [max_rows, max_cols](const int row, const int col) {
return (row < max_rows && row >= 0 && col < max_cols && col >=0);
};
for (int row = 0; row < heights.size(); row++) {
for (int col = 0; col < heights[0].size(); col++) {
for (const auto& motion : motions) {
int score = 0;
auto current = std::vector<int>{row + motion[0], col + motion[1]};
while(in_bounds(current[0], current[1]) && heights[row][col] > heights[current[0]][current[1]]) {
score++;
current[0] += motion[0];
current[1] += motion[1];
}
if (in_bounds(current[0], current[1])) score++;
scores[row][col] *= score;
}
}
}
int best_score = 0;
for (const auto row : scores) {
for(const auto ele : row) {
best_score = std::max(best_score, ele);
}
}
std::cout << best_score << '\n';
return 0;
}