-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.cpp
More file actions
157 lines (143 loc) · 4.33 KB
/
Copy pathday8.cpp
File metadata and controls
157 lines (143 loc) · 4.33 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define LOG(x) std::cout << x << std::endl;
const std::string inputFilePath = "inputs/day8.txt";
std::vector<std::vector<int>> treeGrid; // Assuming square input
std::vector<std::vector<int>> visibleTreeGrid; // Assuming square input.
void fillMatrixWith0(std::vector<std::vector<int>> &matrix, const int rowSize){
for(int y=0;y < rowSize; y++){
std::vector<int> row;
for(int x=0;x < rowSize; x++){
row.push_back(0);
}
matrix.push_back(row);
}
}
int checkTreeVisible(int x, int y, int &maxHeight){
int visibleTrees = 0;
if (treeGrid[y][x] > maxHeight)
{
maxHeight = treeGrid[y][x];
if (visibleTreeGrid[y][x] == 0)
{
visibleTreeGrid[y][x] = 1;
visibleTrees++;
}
}
return visibleTrees;
}
int main(){
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string row;
// Construct input matrix based on row size, fill it with 0's
std::getline(ifs, row);
const size_t rowSize = row.length();
fillMatrixWith0(treeGrid, rowSize);
fillMatrixWith0(visibleTreeGrid, rowSize);
ifs.seekg (0, ifs.beg); // Reset to begining
for(int y = 0; std::getline(ifs, row); y++){
for(int x=0;x < row.size();x++){
treeGrid[y][x] = row[x] - 48; // Substract 48 to convert the ASCII value of the char to the actual number we want
}
}
// Iterate from all 4 sides
int visibleTrees = 0;
for(int y = 0; y < rowSize; y++){
//left-right
int maxHeight = treeGrid[y][0];
visibleTreeGrid[y][0] = 1;
visibleTrees++;
int x = 1;
while(x < rowSize - 1){
visibleTrees += checkTreeVisible(x,y,maxHeight);
x++;
}
//right-left
maxHeight = treeGrid[y][x];
visibleTreeGrid[y][x] = 1;
visibleTrees++;
while(--x > 0){
visibleTrees += checkTreeVisible(x,y,maxHeight);
x--;
}
}
for(int x = 1; x < rowSize - 1; x++){
//up-down
int maxHeight = treeGrid[0][x];
if (visibleTreeGrid[0][x] == 0){
visibleTreeGrid[0][x] = 1;
visibleTrees++;
}
int y = 1;
while(y < rowSize - 1){
visibleTrees += checkTreeVisible(x,y,maxHeight);
y++;
}
//down-up
maxHeight = treeGrid[y][x];
if (visibleTreeGrid[y][x] == 0){
visibleTreeGrid[y][x] = 1;
visibleTrees++;
}
while(y > 0){
visibleTrees += checkTreeVisible(x,y,maxHeight);
y--;
}
}
// Ex2
int maxScenicScore = 0;
for(int y=0; y < rowSize;y++){
for(int x=0; x < rowSize;x++){
const int treeHeight = treeGrid[y][x];
std::vector<int> viewingDistances;
// check right
int rightVD = 0;
int i=x;
while(++i < rowSize){
rightVD += 1;
if(treeGrid[y][i] >= treeHeight){
break;
}
}
// check left
int leftVD = 0;
i = x;
while(--i >= 0){
leftVD += 1;
if(treeGrid[y][i] >= treeHeight){
break;
}
}
// check down
int downVD = 0;
int j=y;
while(++j < rowSize){
downVD += 1;
if(treeGrid[j][x] >= treeHeight){
break;
}
}
// check up
int upVD = 0;
j = y;
while(--j >= 0){
upVD += 1;
if(treeGrid[j][x] >= treeHeight){
break;
}
}
// after all checked
viewingDistances = {rightVD, leftVD, downVD, upVD};
int treeScenicScore = 1;
for(int i=0; i < viewingDistances.size() ;i++){
treeScenicScore = treeScenicScore*viewingDistances[i];
}
maxScenicScore = __max(maxScenicScore, treeScenicScore);
}
}
std::cout << "--Ex1 Output: " << visibleTrees << std::endl;
std::cout << "--Ex2 Output: " << maxScenicScore << std::endl;
return 0;
}