-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day21.cs
334 lines (308 loc) · 10.4 KB
/
Day21.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2016
{
class Day21 : Day
{
public dynamic Input
{
get
{
return new
{
start = "abcdefgh",
part2start = "fbgdceah",
operations =
@"swap position 7 with position 1
swap letter e with letter d
swap position 7 with position 6
move position 4 to position 0
move position 1 to position 4
move position 6 to position 5
rotate right 1 step
swap letter e with letter b
reverse positions 3 through 7
swap position 2 with position 6
reverse positions 2 through 4
reverse positions 1 through 4
reverse positions 5 through 7
rotate left 2 steps
swap letter g with letter f
rotate based on position of letter a
swap letter b with letter h
swap position 0 with position 3
move position 4 to position 7
rotate based on position of letter g
swap letter f with letter e
move position 1 to position 5
swap letter d with letter e
move position 5 to position 2
move position 6 to position 5
rotate right 6 steps
rotate left 4 steps
reverse positions 0 through 3
swap letter g with letter c
swap letter f with letter e
reverse positions 6 through 7
move position 6 to position 1
rotate left 2 steps
rotate left 5 steps
swap position 3 with position 6
reverse positions 1 through 5
rotate right 6 steps
swap letter a with letter b
reverse positions 3 through 4
rotate based on position of letter f
swap position 2 with position 6
reverse positions 5 through 6
swap letter h with letter e
reverse positions 0 through 4
rotate based on position of letter g
rotate based on position of letter d
rotate based on position of letter b
swap position 5 with position 1
rotate based on position of letter f
move position 1 to position 5
rotate right 0 steps
rotate based on position of letter e
move position 0 to position 1
swap position 7 with position 2
rotate left 3 steps
reverse positions 0 through 1
rotate right 7 steps
rotate right 5 steps
swap position 2 with position 0
swap letter g with letter a
rotate left 0 steps
rotate based on position of letter f
swap position 5 with position 1
rotate right 0 steps
rotate left 5 steps
swap letter e with letter a
swap position 5 with position 4
reverse positions 2 through 5
swap letter e with letter a
swap position 3 with position 7
reverse positions 0 through 2
swap letter a with letter b
swap position 7 with position 1
move position 1 to position 6
rotate right 1 step
reverse positions 2 through 6
rotate based on position of letter b
move position 1 to position 0
swap position 7 with position 3
move position 6 to position 5
rotate right 4 steps
reverse positions 2 through 7
reverse positions 3 through 4
reverse positions 4 through 5
rotate based on position of letter f
reverse positions 0 through 5
reverse positions 3 through 4
move position 1 to position 2
rotate left 4 steps
swap position 7 with position 6
rotate right 1 step
move position 5 to position 2
rotate right 5 steps
swap position 7 with position 4
swap letter a with letter e
rotate based on position of letter e
swap position 7 with position 1
swap position 7 with position 3
move position 7 to position 1
swap position 7 with position 4"
};
}
}
public string Part1(dynamic input)
{
char[] chars = ((string)input.start).ToCharArray();
foreach (var op in Utils.splitLines((string)input.operations))
{
var parts = op.Split(' ');
switch (parts[0])
{
case "swap":
if (parts[1] == "position")
{
chars = swapPosition(chars, int.Parse(parts[2]), int.Parse(parts[5]));
}
else
{
chars = swapLetters(chars, parts[2][0], parts[5][0]);
}
break;
case "reverse":
chars = reverse(chars, int.Parse(parts[2]), int.Parse(parts[4]));
break;
case "rotate":
if (parts[1] != "based")
{
chars = rotate(chars, (parts[1] == "left" ? -1 : 1) * int.Parse(parts[2]));
}
else
{
chars = rotateByChar(chars, parts[6][0]);
}
break;
case "move":
chars = move(chars, int.Parse(parts[2]), int.Parse(parts[5]));
break;
}
// Console.WriteLine(op + " -> " + new string(chars));
}
return new string(chars);
}
private char[] rotateByChar(char[] chars, char v)
{
var index = Array.IndexOf(chars, v);
return rotate(chars, 1 + index + (index >= 4 ? 1 : 0));
}
private char[] move(char[] chars, int from, int to)
{
var ret = (char[])chars.Clone();
for (int i = 0; i < chars.Length; i++)
{
if (i == to)
{
ret[i] = chars[from];
}
if (i >= from && i < to)
{
ret[i] = chars[i + 1];
}
if (i > to && i <= from)
{
ret[i] = chars[i - 1];
}
}
return ret;
}
private char[] rotate(char[] chars, int v)
{
var ret = (char[])chars.Clone();
for (int i = 0; i < ret.Length; i++)
{
ret[(i + v + ret.Length) % ret.Length] = chars[i];
}
return ret;
}
private char[] reverse(char[] chars, int v1, int v2)
{
var ret = (char[])chars.Clone();
for (int i = v1; i <= v2; i++)
{
ret[i] = chars[v1 + v2 - i];
}
return ret;
}
private char[] swapLetters(char[] chars, char v1, char v2)
{
var ret = (char[])chars.Clone();
for (int i = 0; i < ret.Length; i++)
{
if (chars[i] == v1)
{
ret[i] = v2;
}
if (chars[i] == v2)
{
ret[i] = v1;
}
}
return ret;
}
private char[] swapPosition(char[] chars, int v1, int v2)
{
var ret = (char[])chars.Clone();
for (int i = 0; i < ret.Length; i++)
{
if (i == v1)
{
ret[i] = chars[v2];
}
if (i == v2)
{
ret[i] = chars[v1];
}
}
return ret;
}
public string Part2(dynamic input)
{
char[] chars = ((string)input.part2start).ToCharArray();
foreach (var op in Utils.splitLines((string)input.operations).Reverse())
{
var before = (char[])chars.Clone();
var parts = op.Split(' ');
switch (parts[0])
{
case "swap":
if (parts[1] == "position")
{
chars = swapPosition(chars, int.Parse(parts[2]), int.Parse(parts[5]));
}
else
{
chars = swapLetters(chars, parts[2][0], parts[5][0]);
}
break;
case "reverse":
chars = reverse(chars, int.Parse(parts[2]), int.Parse(parts[4]));
break;
case "rotate":
if (parts[1] != "based")
{
chars = rotate(chars, (parts[1] == "left" ? 1 : -1) * int.Parse(parts[2]));
}
else
{
chars = rotateByCharReverse(chars, parts[6][0]);
}
break;
case "move":
chars = move(chars, int.Parse(parts[5]), int.Parse(parts[2]));
break;
}
Debug.Assert(Part1(new { start = new string(chars), operations = op }) == new string(before));
// Console.WriteLine(op + " -> " + new string(chars));
}
return new string(chars);
}
private char[] rotateByCharReverse(char[] chars, char v)
{
for (int i = 0; i < chars.Length; i++)
{
int rotation = 1 + i + (i > 3 ? 1 : 0);
var rotated = rotate(chars, -rotation);
if (chars.SequenceEqual(rotateByChar(rotated, v)))
{
return rotated;
}
}
throw new Exception("ach");
}
public void Test()
{
var operations = @"swap position 4 with position 0
swap letter d with letter b
reverse positions 0 through 4
rotate left 1 step
move position 1 to position 4
move position 3 to position 0
rotate based on position of letter b
rotate based on position of letter d";
Utils.Test(Part1, new { start = "abcde", operations = operations }, "decab");
Utils.Test(Part1, new { start = "deabc", operations = operations }, "decab");
Utils.Test(Part2, new { part2start = "decab", operations = operations }, "abcde");
var decode = Part2(new { part2start = "egafbchd", operations = Input.operations });
Utils.Test(Part1, new { start = decode, operations = Input.operations }, "egafbchd");
}
}
}