-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
62 lines (44 loc) · 1.2 KB
/
code.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 <fstream>
#include <vector>
#include <filesystem>
#include <string>
int main () {
if ( !std::filesystem::exists("input") ){
std::cout << "input file does not exist" << std::endl;
return 1;
}
std::ifstream input("input");
/* --- Part 1 --- */
int position = 0, depth = 0;
std::vector<std::pair<std::string, int>> data;
std::string direction; int step;
while ( input >> direction >> step ) {
data.push_back(std::make_pair(direction,step));
}
for (int i = 0; i < data.size(); i++) {
if ( data[i].first == "forward" ) {
position += data[i].second;
} else if (data[i].first == "down") {
depth += data[i].second;
} else if (data[i].first == "up") {
depth -= data[i].second;
}
}
std::cout << "Part1: " << position * depth << std::endl;
/* --- Part 2 --- */
int aim = 0;
position = 0, depth = 0;
for (int i = 0; i < data.size(); i++) {
if ( data[i].first == "forward" ) {
position += data[i].second;
depth += aim * data[i].second;
} else if (data[i].first == "down") {
aim += data[i].second;
} else if (data[i].first == "up") {
aim -= data[i].second;
}
}
std::cout << "Part2: " << position * depth << std::endl;
return 0;
}