-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.cs
86 lines (74 loc) · 2.26 KB
/
Data.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RegExtract;
namespace AdventToolkit.Extensions
{
public static class Data
{
public static IEnumerable<string> Join(this IEnumerable<string[]> ie, string sep = null)
{
if (sep == null) return ie.Select(strings => string.Concat(strings));
return ie.Select(strings => string.Join(sep, strings));
}
public static string Str(this IEnumerable<char> chars)
{
var b = new StringBuilder();
foreach (var c in chars)
{
b.Append(c);
}
return b.ToString();
}
public static string[] Csv(this string s, bool space = false)
{
return space ? s.Split(", ") : s.Split(',');
}
public static IEnumerable<int> Ints(this IEnumerable<string> strings)
{
return strings.Select(int.Parse);
}
public static T Repeat<T>(this T t, Func<T, T> func, int amount)
{
for (var i = 0; i < amount; i++)
{
t = func(t);
}
return t;
}
public static int Product(this IEnumerable<int> ints)
{
return ints.Aggregate((a, b) => a * b);
}
public static long LongProduct(this IEnumerable<int> ints)
{
return ints.Select(i => (long) i).Aggregate((a, b) => a * b);
}
public static IEnumerable<T> Get<T>(this T[] t, IEnumerable<int> indices)
{
return indices.Select(i => t[i]);
}
public static void Swap<T>(this T[] arr, int a, int b)
{
var t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
public static void Swap<T>(ref T a, ref T b)
{
var t = a;
a = b;
b = t;
}
public static IEnumerable<T> FromFormat<T>(this IEnumerable<string> strings, string regex)
{
return strings.Select(s => s.Extract<T>(regex));
}
public static bool Search<T>(this T[] arr, T t, out int i)
{
i = Array.IndexOf(arr, t);
return i > -1;
}
}
}