-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_12a.cpp
49 lines (47 loc) · 1.1 KB
/
day_12a.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
#include <fstream>
#include <iostream>
#include <vector>
int main() {
std::ifstream file{"../input/day_12_input"};
std::string com_inst;
int dir = 0;
int pos_x = 0;
int pos_y = 0;
while (std::getline(file, com_inst)) {
char inst = com_inst[0];
const int mag = std::stoi(com_inst.substr(1, com_inst.size() - 1));
if (inst == 'F') {
if (dir == 0)
inst = 'E';
else if (dir == 1)
inst = 'N';
else if (dir == 2)
inst = 'W';
else if (dir == 3)
inst = 'S';
else {
std::cout << dir << " This should not happen" << '\n';
}
}
if (inst == 'N') {
pos_y += mag;
} else if (inst == 'S') {
pos_y -= mag;
} else if (inst == 'E') {
pos_x += mag;
} else if (inst == 'W') {
pos_x -= mag;
} else if (inst == 'L') {
dir += mag / 90;
dir %= 4;
} else if (inst == 'R') {
dir -= mag / 90;
dir += 4;
dir %= 4;
} else {
std::cout << "This should also not happen" << '\n';
}
}
std::cout << std::abs(pos_x) + std::abs(pos_y) << '\n';
return 0;
}