-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.cpp
83 lines (77 loc) · 1.45 KB
/
08.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <string>
#include "aoc/aoc.h"
using namespace std;
int main(int argc, char* argv[])
{
bool partb = AOC::PartB(argv[0]);
vector<string> forest = AOC::readPicture(cin);
int h = forest.size(), w = forest[0].size();
auto inside = [=](int i, int j)
{
return i >= 0 && i < h && j >= 0 && j < w;
};
if(!partb)
{
vector<vector<int>> v;
v.resize(h, vector<int>(w, 0));
char curr;
auto update = [&](int i, int j)
{
if(curr < forest[i][j])
{
v[i][j] = 1;
curr = forest[i][j];
}
};
for(int i = 0; i < h; i++)
{
curr = ' ';
for(int j = 0; j < w; j++)
update(i, j);
curr = ' ';
for(int j = w-1; j >= 0; j--)
update(i, j);
}
for(int j = 0; j < w; j++)
{
curr = ' ';
for(int i = 0; i < h; i++)
update(i, j);
curr = ' ';
for(int i = h-1; i >= 0; i--)
update(i, j);
}
int count = 0;
for(auto& row : v) for(int x : row) count += x;
cout << count << endl;
}
else
{
int maxsc = 0;
int dir[] = {0, 1, 0, -1, 0};
for(int i = 1; i < h-1; i++)
{
for(int j = 1; j < w-1; j++)
{
int sc = 1;
for(int d = 0; d < 4; d++)
{
int di = dir[d], dj = dir[d+1], ni = i, nj = j, x = 0;
do
{
++x;
ni += di;
nj += dj;
} while(inside(ni, nj) && forest[ni][nj] < forest[i][j]);
if(!inside(ni, nj)) --x;
sc *= x;
}
maxsc = max(maxsc, sc);
}
}
cout << maxsc << endl;
}
return 0;
}