-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8a.cpp
145 lines (127 loc) · 2.93 KB
/
day8a.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
#include "utils.hpp"
class Tree
{
private:
int _height = 0;
int _visibility = 0;
public:
Tree(/* args */);
~Tree();
void set_height(int height);
int get_height();
void add_visibility(int input);
int get_visibility();
};
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;
}
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 count = 0;
for (int a = 0; a < array_size; a++)
{
for (int b = 0; b < array_size; b++)
{
if (visibility[a][b].get_visibility() != 0)
{
count++;
}
}
}
std::cout << count << "\n";
}