-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.2.cpp
85 lines (79 loc) · 2.67 KB
/
Day9.2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <utility>
#include <tuple>
#include <set>
#include <vector>
#define numOfTails 10
template <typename T,typename U>
std::pair<T,U> operator-(const std::pair<T,U> & l,const std::pair<T,U> & r) {
return {l.first-r.first,l.second-r.second};
}
using namespace std;
vector<pair<int, int>> positions;
set<pair<int,int>> tailPositions;
void moveTail() {
for (int i = 1; i < numOfTails; i++) {
tailPositions.insert(positions[numOfTails - 1]);
auto temp = positions[i] - positions[i-1];
if (temp.second == 2) {
if(temp.first == 2)
positions[i] = {positions[i - 1].first +1, positions[i - 1].second + 1};
else if(temp.first == -2)
positions[i] = {positions[i - 1].first -1, positions[i - 1].second + 1};
else
positions[i] = {positions[i - 1].first, positions[i - 1].second + 1};
}
else if (temp.second == -2) {
if(temp.first == 2)
positions[i] = {positions[i - 1].first + 1, positions[i - 1].second - 1};
else if(temp.first == -2)
positions[i] = {positions[i - 1].first - 1, positions[i - 1].second - 1};
else
positions[i] = {positions[i - 1].first, positions[i - 1].second - 1};
}
else if (temp.first == 2)
{
positions[i] = {positions[i - 1].first + 1, positions[i - 1].second};
} else if (temp.first == -2) {
positions[i] = {positions[i - 1].first - 1, positions[i - 1].second};
}
else break;
}
}
int main() {
positions.resize(numOfTails);
fill(positions.begin(),positions.end(),pair<int,int>{0,0});
string input;
fstream file;
file.open("Day9.txt");
tailPositions.insert(pair<int,int>(0,0));
while(getline(cin, input)) {
if (input == "x")
break;
char direction = input.at(0);
int howMany = stoi(input.substr(input.find(' ') + 1, input.size() - 1));
for (int i = 0; i < howMany; i++) {
switch (direction) {
case 'D':
positions[0].second--;
break;
case 'U':
positions[0].second++;
break;
case 'R':
positions[0].first++;
break;
case 'L':
positions[0].first--;
break;
default:
break;
}
moveTail();
}
}
tailPositions.insert(positions[numOfTails-1]);
std::cout << tailPositions.size() << std::endl;
return 0;
}