-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2222.cc
320 lines (308 loc) Β· 5.83 KB
/
aoc2222.cc
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "iostream"
#include "sstream"
#include "vector"
using namespace std;
string split_movement(string&);
bool str_is_number(const string&);
int a_mod_b(int a, int b) { return (b + (a % b)) % b; }
int solve(vector<string>& G, string& movement);
int solve2(vector<string>& G, string& movement);
int main()
{
vector<string> G;
string s;
int i;
int R, C, W = 0;
while (!cin.eof() && getline(cin, s))
{
if (s == "")
break ;
G.push_back(s);
W = max((int) s.length(), W);
}
for (string &x: G)
{
i = -1;
int len = (int) x.length();
while (++i < W - len)
x += ' ';
}
cin >> s;
string movement = split_movement(s);
int res, res2;
res = solve(G, movement);
cout << "\nStar 1: " << res << endl;
res2 = solve2(G, movement);
cout << "\nStar 2: " << res2 << endl;
}
// part 2
int solve2(vector<string>& G, string& movement)
{
int r = 0;
int c = 0;
while (G[0][c] != '.') ++c;
cout << "(starting point) " << r << ' ' << c << endl;
int C = (int) G[0].size();
int R = (int) G.size();
int dr = 0;
int dc = 1; // current facing: East
int i, rr, cc;
string s;
stringstream ss(movement);
while (!ss.eof() && ss >> s)
{
if (str_is_number(s))
{
int val = stoi(s);
i = -1;
while (++i < val)
{
int ddr = dr;
int ddc = dc;
rr = r + dr;
cc = c + dc;
// Case: ba .. ab
// upper ba...lower ab...moving left
if (0 <= rr && rr < 50 && cc == 49 && dc == -1)
{
dc = 1;
rr = 149 - rr;
cc = 0;
}
// lower ab...upper ba...moving left
if (100 <= rr && rr < 150 && cc < 0 && dc == -1)
{
dc = 1;
rr = 149 - rr;
cc = 50;
}
// Case: be ... be
// upper be...lower be...moving on Up
if (rr < 0 && 50 <= cc && cc < 100 && dr == -1)
{
dr = 0;
dc = 1;
rr = cc + 100;
cc = 0;
}
// lower be...upper be...moving Left
if (cc < 0 && 150 <= rr && rr < 200 && dc == -1)
{
dr = 1;
dc = 0;
cc = rr - 100;
rr = 0;
}
// Case: eD ... eD
// upper eD...lower eD...moving Up
if (rr < 0 && 100 <= cc && cc < 150 && dr == -1)
{
rr = 199;
cc = cc - 100;
}
// lower eD...upper eD...moving Down
if (200 <= rr && 0 <= cc && cc < 50 && dr == 1)
{
rr = 0;
cc += 100;
}
// Case: Dc ... cD
// upper Dc...lower cD...moving Right (dc is 1 to be flipped)
if (cc >= 150 && 0 <= rr && rr < 50 && dc == 1)
{
dc = -1;
rr = 149 - rr;
cc = 99;
}
// lower cD...upper Dc...moving right (dc 1 to -1)
if (cc == 100 && 100 <= rr && rr < 150 && dc == 1)
{
dc = -1;
rr = 149 - rr;
cc = 149;
}
// Case: a...
// a dotted-edge 45-clockwise . moving Up
if (0 <= cc && cc < 50 && rr == 99 && dr == -1)
{
dr = 0;
dc = 1;
rr = cc + 50;
cc = 50;
}
// a dotted-edge 45-counterclockwise . moving left
if (cc == 49 && 50 <= rr && rr <= 100 && dc == -1)
{
dr = 1;
dc = 0;
cc = rr - 50;
rr = 100;
}
// Case: D...
// D dotted-edge 45-clockwise . moving down
if (rr == 150 && 50 <= cc && cc < 100 && dr == 1)
{
dr = 0;
dc = -1;
rr = cc + 100;
cc = 49;
}
// D dotted-edge 45-counterclockwise . moving right
if (cc == 50 && 150 <= rr && rr < 200 && dc == 1)
{
dr = -1;
dc = 0;
cc = rr - 100;
rr = 149;
}
// Case: c...
// c dotted-edge 45-clockwise . moving down
if (rr == 50 && 100 <= cc && cc < 150 && dr == 1)
{
dr = 0;
dc = -1;
rr = cc - 50;
cc = 99;
}
// c dotted-edge 45-counterclockwise . moving right
if (50 <= rr && rr < 100 && cc == 100 && dc == 1)
{
dr = -1;
dc = 0;
cc = rr + 50;
rr = 49;
}
// hit the wall
if (G[rr][cc] == '#')
{
dr = ddr;
dc = ddc;
break ;
}
r = rr;
c = cc;
}
}
if (s == "L")
{
swap(dr, dc);
dr *= -1;
}
if (s == "R")
{
swap(dr, dc);
dc *= -1;
}
}
cout << "(final facing) " << dr << ' ' << dc << endl;
int x = -1;
if (dr && dc == 1)
x = 1;
if (dr && dc ^ 1)
x = 3;
if (!dr && dc == 1)
x = 1;
if (!dr && dc ^ 1)
x = 2;
int res = 1000 * (r + 1) + 4 * (c + 1) + x;
cout << "(res) " << res << " at " << R << ',' << C << endl;
return res;
}
// part 1
int solve(vector<string>& G, string& movement)
{
int r = 0;
int c = 0;
while (G[0][c] != '.') ++c;
cout << "(starting point) " << r << ' ' << c << endl;
int C = (int) G[0].size();
int R = (int) G.size();
int dr = 0;
int dc = 1; // current facing: East
int i, rr, cc;
string s;
stringstream ss(movement);
while (!ss.eof() && ss >> s)
{
if (str_is_number(s))
{
int val = stoi(s);
i = -1;
while (++i < val)
{
rr = r;
cc = c;
while (1)
{
rr = a_mod_b(dr + rr, R);
cc = a_mod_b(dc + cc, C);
if (G[rr][cc] != ' ')
break ;
}
if (G[rr][cc] == '#')
break ;
r = rr;
c = cc;
}
}
if (s == "L")
{
swap(dr, dc);
dr *= -1;
}
if (s == "R")
{
swap(dr, dc);
dc *= -1;
}
}
cout << "(final facing) " << dr << ' ' << dc << endl;
int x = -1;
if (dr && dc == 1)
x = 1;
if (dr && dc ^ 1)
x = 3;
if (!dr && dc == 1)
x = 0;
if (!dr && dc ^ 1)
x = 2;
int res = 1000 * (r + 1) + 4 * (c + 1) + x;
cout << "(res) " << res << " at " << R << ',' << C << endl;
return res;
}
//
string split_movement(string& s)
{
stringstream ss(s);
char c;
string S;
while (!ss.eof())
{
ss >> c;
if (!(c <= '9' && c >= '0'))
{
S += c;
S += ' ';
continue ;
}
string temp;
temp += c;
while (1)
{
if (!(ss.peek() <= '9' && ss.peek() >= '0'))
break ;
ss >> c;
temp += c;
continue ;
}
S += temp;
S += ' ';
}
return S;
}
bool str_is_number(const string & s)
{
string::const_iterator it = s.begin();
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}