-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
376 lines (314 loc) · 9.64 KB
/
Extensions.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
namespace AOC;
public static class Extensions
{
public static int ToInt(this string s)
{
return Int32.Parse(s);
}
public static long ToLong(this string s)
{
return Int64.Parse(s);
}
public static int ToInt(this char c)
{
return c.ToString().ToInt();
}
public static bool IsDigit(this char c)
{
return Char.IsDigit(c);
}
public static IEnumerable<string> Words(this string s)
{
return s.Split(" ", StringSplitOptions.RemoveEmptyEntries);
}
public static string Join(this IEnumerable<char> chars)
{
return new string(chars.ToArray());
}
public static string Join(this IEnumerable<string> strings, string separator = "")
{
return String.Join(separator, strings);
}
public static T Second<T>(this IEnumerable<T> items)
{
return items.ElementAt(1);
}
public static T Third<T>(this IEnumerable<T> items)
{
return items.ElementAt(2);
}
public static T Fourth<T>(this IEnumerable<T> items)
{
return items.ElementAt(3);
}
public static bool IsEmpty<T>(this IEnumerable<T> items)
{
return !items.Any();
}
public static bool NonEmpty<T>(this IEnumerable<T> items)
{
return !items.IsEmpty();
}
public static long Product<T>(this IEnumerable<T> items, Func<T, long> selector)
{
return items.Select(selector).Aggregate((a, b) => a * b);
}
public static long Product(this IEnumerable<long> items)
{
return items.Aggregate((a, b) => a * b);
}
public static IEnumerable<T> ToEnumerable<T>(this T item)
{
yield return item;
}
public static IEnumerable<T> Concat<T>(this T item, IEnumerable<T> items)
{
return new[] { item }.Concat(items);
}
public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> items)
{
return items.SelectMany(i => i);
}
public static IEnumerable<IEnumerable<T>> SplitBy<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
var subsequence = new List<T>();
foreach (var item in items)
{
if (predicate(item))
{
if (subsequence.NonEmpty())
{
yield return subsequence;
subsequence = new List<T>();
}
}
else
{
subsequence.Add(item);
}
}
if (subsequence.NonEmpty())
{
yield return subsequence;
}
}
public static IEnumerable<int> To(this int start, int end)
{
var increment = start > end ? -1 : 1;
var boundary = end + increment;
for (var i = start; i != boundary; i += increment)
{
yield return i;
}
}
public static IEnumerable<long> To(this long start, long end)
{
var increment = start > end ? -1 : 1;
var boundary = end + increment;
for (var i = start; i != boundary; i += increment)
{
yield return i;
}
}
}
public record Vector2(long X, long Y)
{
public static readonly Vector2 Zero = new Vector2(0, 0);
public static readonly Vector2 One = new Vector2(1, 1);
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly IEnumerable<Vector2> StraightDirections = new[] { Up, Down, Left, Right };
public static readonly IEnumerable<Vector2> DiagonalDirections = new[] { Up.Add(Left), Up.Add(Right), Down.Add(Left), Down.Add(Right) };
public static readonly IEnumerable<Vector2> EightDirections = StraightDirections.Concat(DiagonalDirections).ToList();
public override string ToString()
{
return $"({X}, {Y})";
}
}
public static class Vector2Extensions
{
public static bool LessOrEqual(this Vector2 a, Vector2 b)
{
return a.X <= b.X && a.Y <= b.Y;
}
public static bool GreaterOrEqual(this Vector2 a, Vector2 b)
{
return a.X >= b.X && a.Y >= b.Y;
}
public static bool In(this Vector2 v, Vector2 min, Vector2 max)
{
return v.GreaterOrEqual(min) && v.LessOrEqual(max);
}
public static bool In<T>(this Vector2 v, Grid2<T> grid)
{
return v.In(grid.Min, grid.Max);
}
public static long ManhattanLength(this Vector2 a)
{
return a.ManhattanDistance(Vector2.Zero);
}
public static long ManhattanDistance(this Vector2 a, Vector2 b)
{
return Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y);
}
public static Vector2 Inverse(this Vector2 v)
{
return v.Multiply(-1);
}
public static Vector2 Sign(this Vector2 v)
{
return new Vector2(Math.Sign(v.X), Math.Sign(v.Y));
}
public static Vector2 Abs(this Vector2 v)
{
return new Vector2(Math.Abs(v.X), Math.Abs(v.Y));
}
public static Vector2 TurnClockwise(this Vector2 v)
{
return new Vector2(v.Y, -v.X);
}
public static Vector2 Multiply(this Vector2 v, long factor)
{
return new Vector2(v.X * factor, v.Y * factor);
}
public static Vector2 Add(this Vector2 a, Vector2 b)
{
return new Vector2(a.X + b.X, a.Y + b.Y);
}
public static Vector2 Subtract(this Vector2 a, Vector2 b)
{
return a.Add(b.Inverse());
}
public static Vector2 Divide(this Vector2 a, Vector2 b)
{
return new Vector2(a.X / b.X, a.Y / b.Y);
}
public static Vector2 Mod(this Vector2 a, Vector2 b)
{
return new Vector2(a.X % b.X, a.Y % b.Y);
}
public static Vector2 Min(this Vector2 a, Vector2 b)
{
return new Vector2(
X: Math.Min(a.X, b.X),
Y: Math.Min(a.Y, b.Y)
);
}
public static Vector2 Shorter(this Vector2 a, Vector2 b)
{
return a.ManhattanLength() < b.ManhattanLength() ? a : b;
}
public static Vector2 Max(this Vector2 a, Vector2 b)
{
return new Vector2(
X: Math.Max(a.X, b.X),
Y: Math.Max(a.Y, b.Y)
);
}
public static IEnumerable<Vector2> StraightAdjacent4(this Vector2 v)
{
return Vector2.StraightDirections.Select(d => v.Add(d));
}
public static IEnumerable<Vector2> EightAdjacent(this Vector2 v)
{
return Vector2.EightDirections.Select(d => v.Add(d));
}
public static Vector2 Min(this IEnumerable<Vector2> vectors)
{
return vectors.Aggregate((a, b) => a.Min(b));
}
public static Vector2 Max(this IEnumerable<Vector2> vectors)
{
return vectors.Aggregate((a, b) => a.Max(b));
}
}
public record Vector3(long X, long Y, long Z);
public static class Vector3Extensions
{
public static bool LessOrEqual(this Vector3 a, Vector3 b)
{
return a.X <= b.X && a.Y <= b.Y && a.Z <= b.Z;
}
public static long ManhattanDistance(this Vector3 a, Vector3 b)
{
return Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y) + Math.Abs(a.Z - b.Z);
}
public static Vector2 ProjectXY(this Vector3 a)
{
return new Vector2(a.X, a.Y);
}
public static Vector3 Inverse(this Vector3 v)
{
return v.Multiply(-1);
}
public static Vector3 Multiply(this Vector3 v, long factor)
{
return new Vector3(v.X * factor, v.Y * factor, v.Z * factor);
}
public static Vector3 Add(this Vector3 a, Vector3 b)
{
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
public static Vector3 Subtract(this Vector3 a, Vector3 b)
{
return a.Add(b.Inverse());
}
public static Vector3 Sign(this Vector3 v)
{
return new Vector3(Math.Sign(v.X), Math.Sign(v.Y), Math.Sign(v.Z));
}
public static Vector3 Min(this Vector3 a, Vector3 b)
{
return new Vector3(
X: Math.Min(a.X, b.X),
Y: Math.Min(a.Y, b.Y),
Z: Math.Min(a.Z, b.Z)
);
}
public static Vector3 Max(this Vector3 a, Vector3 b)
{
return new Vector3(
X: Math.Max(a.X, b.X),
Y: Math.Max(a.Y, b.Y),
Z: Math.Max(a.Z, b.Z)
);
}
}
public record Grid2<T>(Dictionary<Vector2, T> Items, Vector2 Min, Vector2 Max, Vector2 Size)
{
public Grid2(Dictionary<Vector2, T> items, Vector2 min, Vector2 max)
: this(items, min, max, max.Subtract(min).Add(Vector2.One)) { }
public Grid2(Dictionary<Vector2, T> items)
: this(items, items.Keys.Min(), items.Keys.Max()) { }
}
public record Grid2(Grid2<char> grid) : Grid2<char>(grid)
{
public Grid2(Dictionary<Vector2, char> items, Vector2 min, Vector2 max)
: this(new Grid2<char>(items, min, max)) { }
}
public static class GridExtensions
{
public static Grid2<T> ToGrid<T>(this List<string> lines, Func<char, Vector2, T> value, Func<char, bool> predicate = null)
{
var items = new Dictionary<Vector2, T>();
for (var l = 0; l < lines.Count; l++)
{
for (var x = 0; x < lines[l].Length; x++)
{
var c = lines[l][x];
if (predicate == null || predicate(c))
{
var position = new Vector2(x, lines.Count - 1 - l);
items.Add(position, value(c, position));
}
}
}
return new Grid2<T>(items, items.Keys.Min(), items.Keys.Max());
}
public static Grid2 ToGrid(this List<string> lines, Func<char, bool> predicate = null)
{
return new Grid2(lines.ToGrid((c, _) => c, predicate));
}
}