-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec9.cpp
More file actions
76 lines (56 loc) · 1.58 KB
/
Dec9.cpp
File metadata and controls
76 lines (56 loc) · 1.58 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
void updateHead(std::pair<int,int>& headPos,char dir){
switch (dir){
case 'R': ++headPos.first;break;
case 'L': --headPos.first;break;
case 'U': ++headPos.second;break;
case 'D': --headPos.second;break;
default:
std::cout<<"Wrong direction";
break;
}
}
void updateTail(std::pair<int,int>& tailPos,std::pair<int,int>& headPos){
int xdif{headPos.first-tailPos.first};
int ydif{headPos.second-tailPos.second};
if (std::abs(xdif)>1 || std::abs(ydif)>1){
tailPos.first += xdif/std::abs(xdif);
tailPos.second += ydif/std::abs(ydif);
}
}
int main(){
bool part_one{false};
//READ DATA
std::ifstream file("data9.txt");
char dir{};
int amount{};
std::pair<int,int> headPos(0,0);
std::vector<std::pair<int,int>> tailPos;
if (part_one){
tailPos.push_back({0,0});
}else{
for (int i=0;i<9;++i){
tailPos.push_back({0,0});
}
}
std::set<std::pair<int,int>> Positions{};
while(file.peek()!=EOF){
file>>dir>>amount;
for (int i=0;i<amount;++i){
updateHead(headPos,dir);
updateTail(tailPos[0],headPos);
if(!part_one){
for (int i=1;i<9;++i){
updateTail(tailPos[i],tailPos[i-1]);
}
}
Positions.insert(tailPos.back());
}
}
std::cout<<Positions.size()<<'\n';
return 0;
}