-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththird.cpp
More file actions
49 lines (45 loc) · 1.41 KB
/
Copy paththird.cpp
File metadata and controls
49 lines (45 loc) · 1.41 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
#include <iostream>
#include <fstream>
#include <String>
#include <vector>
int main() {
std::ifstream file("prompt3.txt");
std::string line;
std::vector<std::string> engine;
std::string row;
std::string currentNumString;
int total = 0;
int currentNum;
bool isPart = false;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
while (std::getline(file, line)) {
engine.push_back(line);
}
for(int i = 0; i < engine.size(); i++) {
row = engine[i];
for(int j = 0; j < row.length(); j++) {
if (isdigit(row[j])) {
currentNumString += row[j];
for (int k = 0; k < 8; k++) {
if (i+dy[k] >= 0 && i+dy[k] < engine.size() && j+dx[k] >= 0 && j+dx[k] < row.length()) {
if (ispunct(engine[i+dy[k]][j+dx[k]]) && engine[i+dy[k]][j+dx[k]] != '.') {
isPart = true;
}
}
}
} else {
if (isPart) {
currentNum = std::stoi(currentNumString);
total += currentNum;
isPart = false;
currentNumString = "";
} else {
currentNumString = "";
}
}
}
}
std::cout << total << std::endl;
return 0;
}