-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec22_part1.cpp
More file actions
217 lines (159 loc) · 5.04 KB
/
Copy pathDec22_part1.cpp
File metadata and controls
217 lines (159 loc) · 5.04 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
void print(const std::vector<std::vector<char>>& map){
std::cout<<" ";
for (int i=0;i<map[0].size();++i){
std::cout<<i%10;
}
int i{};
for (auto it=map.begin();it!=map.end();++it){
std::cout<<(i++)%10<<' ';
for (auto itt=it->begin();itt!=it->end();++itt){
std::cout<<*itt;
}
std::cout<<'\n';
}
std::cout<<'\n';
}
void read_file(std::ifstream& file,std::vector<std::vector<char>>& map,std::string& operations){
std::string str{};
int max_row_size{};
while (getline(file,str)){
if (str.front()==' ' || str.front()=='.' || str.front()=='#'){
std::vector<char> row{};
row.push_back(' ');
for (auto it=str.begin();it!=str.end();++it){
row.push_back(*it);
}
row.push_back(' ');
max_row_size = std::max(max_row_size,static_cast<int>(row.size()));
map.push_back(row);
}else{
operations = str;
}
}
for (auto it=map.begin();it!=map.end();++it){
if (it->size()<max_row_size){
size_t itsize = it->size();
for (int i=0;i<max_row_size-itsize;++i){
it->push_back(' ');
}
}
}
map.push_back(std::vector<char> (max_row_size,' '));
map.insert(map.begin(),std::vector<char> (max_row_size,' '));
}
void starting_point(const std::vector<char>& row,std::pair<int,int>& point){
point.first = 1;
for (auto it=row.begin();it!=row.end();++it){
if (*it=='.'){
point.second = it-row.begin();
break;
}
}
}
void move_row(const std::vector<std::vector<char>>& map,std::pair<int,int>& point,int direction,int value){
//MOVE IN ROW FROM POINT ALONG DIRECTION FOR AN AMOUNT VALUE
for (int i=0;i<value;++i){
char next = map[point.first][point.second+direction];
if (next=='.'){
point.second += direction;
}else if(next=='#'){
break;
}else{
//IF NEXT POINT IS EMPTY, LOOK BACK FOR BEGINNING/END OF ROW
int next_coord{point.second};
while (map[point.first][next_coord]!=' '){
next_coord -= direction;
}
next_coord += direction;
char new_next = map[point.first][next_coord];
if (new_next=='.'){
point.second = next_coord;
}else if (new_next=='#'){
break;
}
}
}
}
void move_col(const std::vector<std::vector<char>>& map,std::pair<int,int>& point,int direction,int value){
//MOVE IN COL FROM POINT ALONG DIRECTION FOR AN AMOUNT VALUE
for (int i=0;i<value;++i){
char next = map[point.first+direction][point.second];
if (next=='.'){
point.first += direction;
}else if(next=='#'){
break;
}else{
//IF NEXT POINT IS EMPTY, LOOK BACK FOR BEGINNING/END OF COL
int next_coord{point.first};
while (map[next_coord][point.second]!=' '){
next_coord -= direction;
}
next_coord += direction;
char new_next = map[next_coord][point.second];
if (new_next=='.'){
point.first = next_coord;
}else if (new_next=='#'){
break;
}
}
}
}
void rotation(std::pair<int,int>& direction,char rotate){
std::vector<std::pair<int,int>> directions {{0,1},{1,0},{0,-1},{-1,0}};
int index{};
for (int i=0;i<4;++i){
if (direction==directions[i]){
index = i;
break;
}
}
if (rotate=='R'){
++index;
if (index==4) index = 0;
}else{
--index;
if (index==-1) index = 3;
}
direction = directions[index];
}
int direction_score(const std::pair<int,int>& direction){
if (direction==std::pair<int,int> {0,1}) return 0;
if (direction==std::pair<int,int> {1,0}) return 1;
if (direction==std::pair<int,int> {0,-1}) return 2;
return 3;
}
int main(){
std::ifstream file("data22.txt");
std::vector<std::vector<char>> map{};
std::string operations{};
read_file(file,map,operations);
print(map);
std::pair<int,int> point{};
starting_point(map[1],point);
std::stringstream opstream{operations};
std::pair<int,int> direction{0,1};
int value{};
char rotate{};
while (opstream.peek()!=EOF){
opstream>>value;
if (direction.first==0){
move_row(map,point,direction.second,value);
}else{
move_col(map,point,direction.first,value);
}
opstream>>rotate;
if (rotate=='X') break;
else{
rotation(direction,rotate);
}
//std::cout<<point.first<<' '<<point.second<<'\n';
}
std::cout<<"Score "<<1000*point.first+4*point.second+direction_score(direction)<<'\n';
return 0;
}