-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththirdPart2.cpp
More file actions
63 lines (60 loc) · 2.02 KB
/
Copy paththirdPart2.cpp
File metadata and controls
63 lines (60 loc) · 2.02 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
#include <iostream>
#include <fstream>
#include <String>
#include <vector>
#include <unordered_map>
int main() {
std::ifstream file("prompt3.txt");
std::string line;
std::vector<std::string> engine;
std::string row;
std::string currentNumString;
std::unordered_map<std::string, std::vector<int>> gearmap = {};
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};
std::string partCoords;
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 (engine[i+dy[k]][j+dx[k]] == '*' && isPart == false) {
isPart = true;
partCoords = std::to_string(i+dy[k]) + "," + std::to_string(j+dx[k]);
}
}
}
} else {
if (isPart) {
currentNum = std::stoi(currentNumString);
if (gearmap.find(partCoords) == gearmap.end()) {
gearmap[partCoords] = {currentNum};
} else {
gearmap[partCoords].push_back(currentNum);
}
isPart = false;
currentNumString = "";
} else {
currentNumString = "";
}
}
}
}
for (auto it = gearmap.begin(); it != gearmap.end(); it++) {
int product;
if (it->second.size() == 2){
product = it->second[0] * it->second[1];
total += product;
}
}
std::cout << total << std::endl;
return 0;
}