-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.cpp
62 lines (47 loc) · 1.36 KB
/
day2.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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int moveSubmarine(const string filename) {
int horizontalPos = 0;
int depth = 0;
ifstream sstream(filename);
for (string ss; getline(sstream, ss); ) {
string dir = ss.substr(0, ss.find(' '));
int dx = stoi(ss.substr(ss.find(' ')+1));
if (dir == "forward") {
horizontalPos += dx;
} else if (dir == "down") {
depth += dx;
} else if (dir == "up") {
depth -= dx;
}
}
return horizontalPos * depth;
}
int altMoveSubmarine(const string filename) {
int horizontalPos = 0;
int depth = 0;
int aim = 0;
ifstream sstream(filename);
for (string ss; getline(sstream, ss); ) {
string dir = ss.substr(0, ss.find(' '));
int dx = stoi(ss.substr(ss.find(' ')+1));
if (dir == "forward") {
horizontalPos += dx;
depth += aim * dx;
} else if (dir == "down") {
aim += dx;
} else if (dir == "up") {
aim -= dx;
}
}
return horizontalPos * depth;
}
int main() {
auto multiple = moveSubmarine("day2.txt");
cout << "Final horizontal x depth = " << multiple << endl;
multiple = altMoveSubmarine("day2.txt");
cout << "Final horizontal x depth = " << multiple << endl;
}