-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.cpp
98 lines (81 loc) · 2.09 KB
/
day8.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
using namespace std;
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <vector>
#include "Helpers/HelperFunctions.h"
int main() {
cout << "Day 8" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2022\inputs\day8.txt)");
string str;
vector<vector<int>> grid;
int xEdge;
int yEdge;
int i = 0;
while (getline(file, str)) {
xEdge = str.length() - 1;
vector<int> row;
for (auto &it : str) {
row.push_back(it - '0');
}
grid.push_back(row);
i++;
}
yEdge = i - 1;
int acc = 0;
int max = 0;
for (int x = 0; x <= xEdge; x++) {
for (int y = 0; y <= yEdge; y++) {
if (x == 0 || x == xEdge || y == 0 || y == yEdge) {
acc++;
} else {
int curr = grid[y][x];
int score = 1;
bool leftVisible = true;
for (int i = x - 1; i >= 0; i--) {
if (curr <= grid[y][i]) {
leftVisible = false;
score *= (x - i);
break;
}
}
if (leftVisible) score *= x;
bool rightVisible = true;
for (int i = x + 1; i <= xEdge; i++) {
if (curr <= grid[y][i]) {
rightVisible = false;
score *= (i - x);
break;
}
}
if (rightVisible) score *= (xEdge - x);
bool topVisible = true;
for (int i = y - 1; i >= 0; i--) {
if (curr <= grid[i][x]) {
topVisible = false;
score *= (y - i);
break;
}
}
if (topVisible) score *= (y);
bool bottomVisible = true;
for (int i = y + 1; i <= yEdge; i++) {
if (curr <= grid[i][x]) {
bottomVisible = false;
score *= (i - y);
break;
}
}
if (bottomVisible) score *= (yEdge - y);
max = score > max ? score : max;
if (leftVisible || rightVisible || topVisible || bottomVisible) {
acc++;
}
}
}
}
cout << "Part 1: " << acc << endl;
cout << "Part 2: " << max << endl;
return 0;
}