-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.cpp
68 lines (65 loc) · 1.97 KB
/
8.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 "common.h"
namespace {
long p1(const auto &input) {
std::set<std::tuple<size_t, size_t>> trees{};
size_t m = input.size(), n = input[0].size();
for (size_t i = 0; i < m; ++i) {
for (auto [j, max] = std::tuple{static_cast<size_t>(0), -1}; j < n; ++j) {
if (const auto &h = input[i][j] - '0'; h > max) {
max = h;
trees.emplace(std::make_tuple(i, j));
}
}
for (auto [j, max] = std::tuple{n, -1}; j-- != 0;) {
if (const auto &h = input[i][j] - '0'; h > max) {
max = h;
trees.emplace(std::make_tuple(i, j));
}
}
}
for (size_t j = 0; j < n; ++j) {
for (auto [i, max] = std::tuple{static_cast<size_t>(0), -1}; i < m; ++i) {
if (const auto &h = input[i][j] - '0'; h > max) {
max = h;
trees.emplace(std::make_tuple(i, j));
}
}
for (auto [i, max] = std::tuple{m, -1}; i-- != 0;) {
if (const auto &h = input[i][j] - '0'; h > max) {
max = h;
trees.emplace(std::make_tuple(i, j));
}
}
}
return trees.size();
}
long p2(const auto &input) {
size_t m = input.size(), n = input[0].size();
long best{0};
for (size_t i = 1; i < m - 1; i++) {
for (size_t j = 1; j < n - 1; j++) {
long h = input[i][j] - '0';
long l{1}, r{1}, t{1}, b{1};
for (size_t k = static_cast<long>(j) - 1;
k > 0 && (input[i][k] - '0') < h; --k)
++l;
for (size_t k = static_cast<long>(j) + 1;
k < n - 1 && (input[i][k] - '0') < h; ++k)
++r;
for (size_t k = static_cast<long>(i) - 1;
k > 0 && (input[k][j] - '0') < h; --k)
++t;
for (size_t k = static_cast<long>(i) + 1;
k < m - 1 && (input[k][j] - '0') < h; ++k)
++b;
best = std::max(best, l * r * t * b);
}
}
return best;
}
} // namespace
int main() {
const auto &input = gb::advent2021::readIn();
gb::advent2021::writeOut(std::to_string(p1(input)));
gb::advent2021::writeOut(std::to_string(p2(input)));
}