-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8b.cpp
217 lines (194 loc) · 4.71 KB
/
day8b.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "utils.hpp"
class Tree
{
private:
int _height = 0;
int _visibility = 0;
std::vector<int> _multi{};
public:
Tree(/* args */);
~Tree();
void set_height(int height);
int get_height();
void add_visibility(int input);
int get_visibility();
void add_multi(int);
int get_total();
};
Tree::Tree(/* args */)
{
_height = 0;
}
Tree::~Tree()
{
}
void Tree::set_height(int height)
{
if (height > 9)
{
std::cout << height
<< "\n";
}
_height = height;
}
void Tree::add_visibility(int input)
{
_visibility += input;
}
int Tree::get_height()
{
return _height;
}
int Tree::get_visibility()
{
return _visibility;
}
void Tree::add_multi(int multiplier)
{
_multi.push_back(multiplier);
}
int Tree::get_total()
{
int value = 1;
for (auto &&x : _multi)
{
value = value * x;
}
return value;
}
int main(int argc, char *argv[])
{
auto input = read_input("day8_input");
static int left = 1;
static int right = 2;
static int up = 4;
static int down = 8;
const int array_size = 99;
// [0][99] -> top right
// [99][0] -> bottom left
Tree visibility[array_size][array_size]{};
int line = 0;
for (auto &&x : input)
{
int pos = 0;
for (auto &&y : x)
{
visibility[line][pos].set_height(y - '0');
pos++;
}
line++;
}
for (int i = 0; i < array_size; i++)
{
int max_l = -1;
int max_r = -1;
int max_u = -1;
int max_d = -1;
for (int j = 0; j < array_size; j++)
{
// viewable from the left
if (max_l != 9)
{
auto cur_l = visibility[i][j].get_height();
if (cur_l > max_l)
{
visibility[i][j].add_visibility(left);
max_l = cur_l;
}
}
// viewable from the right
if (max_r != 9)
{
auto cur_r = visibility[i][array_size - j - 1].get_height();
if (cur_r > max_r)
{
visibility[i][array_size - j - 1].add_visibility(right);
max_r = cur_r;
}
}
// viewable from up
if (max_u != 9)
{
auto cur_u = visibility[j][i].get_height();
if (cur_u > max_u)
{
visibility[j][i].add_visibility(up);
max_u = cur_u;
}
}
// viewable from down
if (max_d != 9)
{
auto cur_d = visibility[array_size - j - 1][i].get_height();
if (cur_d > max_d)
{
visibility[array_size - j - 1][i].add_visibility(down);
max_d = cur_d;
}
}
}
}
int max_scenic = 0;
for (int a = 0; a < array_size; a++)
{
for (int b = 0; b < array_size; b++)
{
auto tree = visibility[a][b];
auto test = tree.get_height();
int l = b;
int r = b;
int u = a;
int d = a;
while (l > 0)
{
l--;
if ((visibility[a][l].get_height() >= test) || (l == 0))
{
tree.add_multi(b - l);
break;
}
}
while (r < array_size)
{
r++;
if (visibility[a][r].get_height() >= test)
{
tree.add_multi(r - b);
break;
}
else if (r == array_size)
{
tree.add_multi(r - b - 1);
}
}
while (u > 0)
{
u--;
if ((visibility[u][b].get_height() >= test) || (u == 0))
{
tree.add_multi(a - u);
break;
}
}
while (d < array_size)
{
d++;
if (visibility[d][b].get_height() >= test)
{
tree.add_multi(d - a);
break;
}
else if (d == array_size)
{
tree.add_multi(d - a - 1);
}
}
if (tree.get_total() > max_scenic)
{
std::cout << a << "," << b << "=" << tree.get_total() << "\n";
max_scenic = tree.get_total();
}
}
}
std::cout << max_scenic << "\n";
}