-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_09a.cpp
50 lines (45 loc) · 1.4 KB
/
day_09a.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
#include <array>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <string>
// TODO: Improve hash function
int main(int argc, char * argv[]) {
std::string input = "../input/day_09_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::array<int, 2> point_H = {0, 0};
std::array<int, 2> point_T = {0, 0};
std::unordered_set<int> s;
while(std::getline(file, line)) {
for (int i = 0; i < std::stoi(line.substr(2, line.size() - 2)); i++) {
if (line[0] == 'U') {
point_H[0] += 1;
} else if (line[0] == 'D') {
point_H[0] -= 1;
} else if (line[0] == 'R') {
point_H[1] += 1;
} else {
point_H[1] -= 1;
}
const auto delta = std::array<int,2>{point_H[0] - point_T[0], point_H[1] - point_T[1]};
if (std::abs(delta[0]) == 2 && std::abs(delta[1]) == 0) {
point_T[0] += delta[0]/2;
} else if (std::abs(delta[0]) == 2 && std::abs(delta[1]) == 1) {
point_T[0] += delta[0]/2;
point_T[1] += delta[1];
} else if (std::abs(delta[0]) == 0 && std::abs(delta[1]) == 2) {
point_T[1] += delta[1]/2;
} else if (std::abs(delta[0]) == 1 && std::abs(delta[1]) == 2) {
point_T[0] += delta[0];
point_T[1] += delta[1]/2;
}
s.insert(point_T[0] * 100000 + point_T[1]);
}
}
std::cout << s.size() << '\n';
return 0;
}