-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_23.cs
309 lines (264 loc) · 8.24 KB
/
2022_23.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
namespace AOC.CSharp;
public static class AOC2022_23
{
public static long Solve1(string[] lines)
{
long result = Solve(lines, 1);
return result;
}
public static long Solve2(string[] lines)
{
long result = Solve(lines, 2);
return result;
}
public static long Solve(string[] lines, int part)
{
char[,] grid = Parse(lines);
List<Func<char[,], XY, Direction?>> tryMoves =
new() { TryMoveNorth, TryMoveSouth, TryMoveWest, TryMoveEast, };
int minRow = int.MaxValue;
int maxRow = 0;
int minCol = int.MaxValue;
int maxCol = 0;
int round = 1;
while (true)
{
Console.WriteLine(round);
List<Move> moves = new();
for (int row = 0; row < grid.GetLength(0); row++)
{
for (int col = 0; col < grid.GetLength(1); col++)
{
XY curr = new(col, row);
if (grid[row, col] == '#')
{
minRow = Math.Min(minRow, row);
maxRow = Math.Max(maxRow, row);
minCol = Math.Min(minCol, col);
maxCol = Math.Max(maxCol, col);
bool isNeighbor = IsElfNeighbor(grid, curr);
if (isNeighbor)
{
Direction? toTry = null;
for (int i = 0; i < tryMoves.Count && toTry == null; i++)
{
toTry = tryMoves[i](grid, curr);
if (toTry.HasValue)
{
XY other = FindOther(curr, toTry.Value);
moves.Add(new Move(curr, other));
}
}
}
}
}
}
Dictionary<XY, int> destCounts = moves
.GroupBy(m => m.To)
.ToDictionary(g => g.Key, g => g.Count());
if (part == 2 && !moves.Any())
{
return round;
}
foreach (Move m in moves)
{
if (destCounts[m.To] == 1)
{
minRow = Math.Min(minRow, m.To.Y);
maxRow = Math.Max(maxRow, m.To.Y);
minCol = Math.Min(minCol, m.To.X);
maxCol = Math.Max(maxCol, m.To.X);
grid[m.To.Y, m.To.X] = '#';
grid[m.From.Y, m.From.X] = '.';
}
}
var newLast = tryMoves[0];
tryMoves.Add(newLast);
tryMoves.RemoveAt(0);
if (part == 1 && round == 10)
{
long result = 0;
(minRow, maxRow, minCol, maxCol) = Trim(grid, minRow, maxRow, minCol, maxCol);
for (int row = minRow; row <= maxRow; row++)
{
for (int col = minCol; col <= maxCol; col++)
{
result += grid[row, col] == '.' ? 1 : 0;
}
}
return result;
}
round++;
}
}
private static Direction? TryMoveNorth(char[,] grid, XY curr)
{
bool canMove = true;
canMove &= Get(grid, curr, Direction.NW) == '.';
canMove &= Get(grid, curr, Direction.N) == '.';
canMove &= Get(grid, curr, Direction.NE) == '.';
return canMove ? Direction.N : null;
}
private static Direction? TryMoveSouth(char[,] grid, XY curr)
{
bool canMove = true;
canMove &= Get(grid, curr, Direction.SW) == '.';
canMove &= Get(grid, curr, Direction.S) == '.';
canMove &= Get(grid, curr, Direction.SE) == '.';
return canMove ? Direction.S : null;
}
private static Direction? TryMoveWest(char[,] grid, XY curr)
{
bool canMove = true;
canMove &= Get(grid, curr, Direction.NW) == '.';
canMove &= Get(grid, curr, Direction.W) == '.';
canMove &= Get(grid, curr, Direction.SW) == '.';
return canMove ? Direction.W : null;
}
private static Direction? TryMoveEast(char[,] grid, XY curr)
{
bool canMove = true;
canMove &= Get(grid, curr, Direction.NE) == '.';
canMove &= Get(grid, curr, Direction.E) == '.';
canMove &= Get(grid, curr, Direction.SE) == '.';
return canMove ? Direction.E : null;
}
private static char[,] Parse(string[] lines)
{
char[,] grid = new char[2500, 2500];
for (int row = 0; row < grid.GetLength(0); row++)
{
for (int col = 0; col < grid.GetLength(1); col++)
{
grid[row, col] = '.';
}
}
for (int row = 0; row < lines.Length; row++)
{
for (int col = 0; col < lines[0].Length; col++)
{
int trueRow = row + 600;
int trueCol = col + 600;
grid[trueRow, trueCol] = lines[row][col];
}
}
return grid;
}
private static bool IsElfNeighbor(char[,] grid, XY curr)
{
bool found = false;
foreach (Direction d in Enum.GetValues<Direction>())
{
char val = Get(grid, curr, d);
found |= val == '#';
}
return found;
}
private static char Get(char[,] grid, XY curr, Direction dir)
{
XY other = FindOther(curr, dir);
return grid[other.Y, other.X];
}
private static (int, int, int, int) Trim(
char[,] grid,
int minRow,
int maxRow,
int minCol,
int maxCol
)
{
for (int row = minRow; row <= maxRow; row++)
{
bool empty = true;
for (int col = minCol; col <= maxCol && empty; col++)
{
empty &= grid[row, col] == '.';
}
if (empty)
{
minRow++;
}
else
{
break;
}
}
for (int row = maxRow; row >= minRow; row--)
{
bool empty = true;
for (int col = minCol; col <= maxCol && empty; col++)
{
empty &= grid[row, col] == '.';
}
if (empty)
{
maxRow--;
}
else
{
break;
}
}
for (int col = minCol; col <= maxCol; col++)
{
bool empty = true;
for (int row = minRow; row <= maxRow; row++)
{
empty &= grid[row, col] == '.';
}
if (empty)
{
minCol++;
}
else
{
break;
}
}
for (int col = minCol; col <= maxCol; col++)
{
bool empty = true;
for (int row = maxRow; row >= minRow; row--)
{
empty &= grid[row, col] == '.';
}
if (empty)
{
maxCol--;
}
else
{
break;
}
}
return (minRow, maxRow, minCol, maxCol);
}
private record Move(XY From, XY To);
private enum Direction
{
N,
NE,
E,
SE,
S,
SW,
W,
NW,
}
private static XY FindOther(XY curr, Direction dir)
{
return dir switch
{
Direction.N => curr with { Y = curr.Y - 1 },
Direction.NE => curr with { X = curr.X + 1, Y = curr.Y - 1 },
Direction.E => curr with { X = curr.X + 1 },
Direction.SE => curr with { X = curr.X + 1, Y = curr.Y + 1 },
Direction.S => curr with { Y = curr.Y + 1 },
Direction.SW => curr with { X = curr.X - 1, Y = curr.Y + 1 },
Direction.W => curr with { X = curr.X - 1 },
Direction.NW => curr with { X = curr.X - 1, Y = curr.Y - 1 },
_ => throw new NotSupportedException(),
};
}
private record XY(int X, int Y);
}