-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
129 lines (120 loc) · 4.38 KB
/
Utils.cs
File metadata and controls
129 lines (120 loc) · 4.38 KB
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
// ############################################################################
// # Galen Lanphier #
// # https://github.com/lanphiergm/AdventOfCodeCS #
// # MIT License. See LICENSE file #
// ############################################################################
using System.Collections.Generic;
namespace AdventOfCode.Puzzles
{
/// <summary>
/// Contains common utility functions
/// </summary>
internal static class Utils
{
/// <summary>
/// Determines if the two matrices are equal
/// </summary>
/// <param name="a">The first matrix to test</param>
/// <param name="b">The second matrix to test</param>
/// <returns>true if every member of the matrix is equal; otherwise, false</returns>
public static bool AreMatricesEqual<T>(T[,] a, T[,] b)
{
for (int i = 0; i <= a.GetUpperBound(0); i++)
{
for (int j = 0; j <= a.GetUpperBound(1); j++)
{
if (!a[i,j].Equals(b[i,j]))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Builds all permutations of the items in the specified list
/// </summary>
/// <typeparam name="T">The type contained in the list</typeparam>
/// <param name="list">The list to permute</param>
/// <returns>The list of all permutations of the original list</returns>
public static List<List<T>> BuildPermutations<T>(List<T> list)
{
var orders = new List<List<T>>();
BuildPermutations(list, orders, 0, list.Count - 1);
return orders;
}
private static void BuildPermutations<T>(List<T> list, List<List<T>> orders, int k, int m)
{
if (k == m)
{
orders.Add(new List<T>(list));
}
else
{
for (int i = k; i <= m; i++)
{
list.Swap(k, i);
BuildPermutations(list, orders, k + 1, m);
list.Swap(k, i);
}
}
}
/// <summary>
/// Flips the specified matrix about the horizontal (first dimension) axis
/// </summary>
/// <typeparam name="T">The type of the items in the matrix</typeparam>
/// <param name="matrix">The matrix to flip</param>
/// <returns>The flipped matrix</returns>
public static T[,] Flip<T>(T[,] matrix)
{
int n = matrix.GetUpperBound(0) + 1;
var flipped = new T[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
flipped[i, j] = matrix[i, n - j - 1];
}
}
return flipped;
}
/// <summary>
/// Flops the specified matrix about the vertical (second dimension) axis
/// </summary>
/// <typeparam name="T">The type of the items in the matrix</typeparam>
/// <param name="matrix">The matrix to flop</param>
/// <returns>The flopped matrix</returns>
public static T[,] Flop<T>(T[,] matrix)
{
int n = matrix.GetUpperBound(0) + 1;
var flipped = new T[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
flipped[i, j] = matrix[n - i - 1, j];
}
}
return flipped;
}
/// <summary>
/// Rotates the specified matrix 90° clockwise
/// </summary>
/// <typeparam name="T">The type of the items in the matrix</typeparam>
/// <param name="matrix">The matrix to rotate</param>
/// <returns>The rotated matrix</returns>
public static T[,] RotateClockwise<T>(T[,] matrix)
{
int n = matrix.GetUpperBound(0) + 1;
var rotated = new T[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
rotated[i, j] = matrix[n - j - 1, i];
}
}
return rotated;
}
}
}