-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC_Day3_P1.cpp
More file actions
53 lines (45 loc) · 1.6 KB
/
AoC_Day3_P1.cpp
File metadata and controls
53 lines (45 loc) · 1.6 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
#include <bits/stdc++.h>
using namespace std;
int main() {
// ifstream fin("input.txt");
string line;
vector<vector<char>> grid;
vector<vector<int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, -1}, {-1, 1}, {-1, -1}, {1, 1}};
while (getline(cin, line)) {
vector<char> row;
for (char c : line) {
row.push_back(c);
}
grid.push_back(row);
}
int ans = 0;
for (int i = 0; i < int(grid.size()); i++) {
for (int j = 0; j < int(grid[0].size()); j++) {
bool symbol = false;
string num = "";
while (int(grid[i][j]) - int('0') < 10 && int(grid[i][j]) - int('0') >= 0 && j < int(grid[0].size())) {
num += grid[i][j];
j++;
}
for (int k = j-1; k >= j-int(num.size()); k--) {
for (vector<int> dir : directions) {
int nRow = i + dir[0];
int nCol = k + dir[1];
if (nRow >= 0 && nRow < int(grid.size()) && nCol >= 0 && nCol < int(grid[0].size())) {
if (grid[nRow][nCol] - int('0') > 10 || grid[nRow][nCol] - int('0') < 0) {
if (grid[nRow][nCol] != '.') {
if (num != "") ans += stoi(num);
symbol = true;
break;
}
}
}
}
if (symbol) {
break;
}
}
}
}
cout << ans << "\n";
}