-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_09b.cpp
64 lines (57 loc) · 1.74 KB
/
day_09b.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
63
64
#include <array>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
// TODO: Improve hash function
void update_pair(const int k1, const int k2, std::array<std::array<int,2>, 10>& knots) {
const auto delta = std::array<int,2>{knots[k1][0] - knots[k2][0], knots[k1][1] - knots[k2][1]};
if (std::abs(delta[0]) == 2 && std::abs(delta[1]) == 0) {
knots[k2][0] += delta[0]/2;
} else if (std::abs(delta[0]) == 2 && std::abs(delta[1]) == 1) {
knots[k2][0] += delta[0]/2;
knots[k2][1] += delta[1];
} else if (std::abs(delta[0]) == 0 && std::abs(delta[1]) == 2) {
knots[k2][1] += delta[1]/2;
} else if (std::abs(delta[0]) == 1 && std::abs(delta[1]) == 2) {
knots[k2][0] += delta[0];
knots[k2][1] += delta[1]/2;
} else if (std::abs(delta[0]) == 2 && std::abs(delta[1]) == 2) {
knots[k2][0] += delta[0]/2;
knots[k2][1] += delta[1]/2;
}
}
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<std::array<int, 2>, 10> knots;
for (auto& row : knots) {
for (auto& ele : row) {
ele = 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') {
knots[0][0] += 1;
} else if (line[0] == 'D') {
knots[0][0] -= 1;
} else if (line[0] == 'R') {
knots[0][1] += 1;
} else {
knots[0][1] -= 1;
}
for (int i = 0; i < knots.size() - 1; i++) {
update_pair(i, i+1, knots);
}
s.insert(knots[9][0] * 100000 + knots[9][1]);
}
}
std::cout << s.size() << '\n';
return 0;
}