-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.cpp
More file actions
56 lines (49 loc) · 1.21 KB
/
day1.cpp
File metadata and controls
56 lines (49 loc) · 1.21 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
#include "day1.hpp"
#include <string>
namespace Day1 {
int apply(const int, const string &);
int solve_day1_pt1(const vector<string> &rotations) {
int current = 50;
int result = 0;
for (const string &rotation : rotations) {
current = apply(current, rotation);
if (current == 0) {
++result;
}
}
return result;
}
int solve_day1_pt2(const vector<string> &rotations) {
int current = 50;
int result = 0;
for (const string &rotation : rotations) {
int amount = stoi(rotation.substr(1));
const int direction = (rotation[0] == 'L') ? -1 : 1;
result += (amount / 100);
amount %= 100;
amount *= direction;
current += amount;
if (current < 0) {
current = 99 + current + 1;
++result;
} else if (current > 99) {
current = current - 99 - 1;
++result;
}
}
return result;
}
int apply(const int current, const string &rotation) {
int amount = stoi(rotation.substr(1));
const int direction = (rotation[0] == 'L') ? -1 : 1;
amount %= 100;
amount *= direction;
int result = current + amount;
if (result < 0) {
result = 99 + result + 1;
} else if (result > 99) {
result = result - 99 - 1;
}
return result;
}
} // namespace Day1