Added Utils namespace

This commit is contained in:
Tiago Garcia 2023-06-08 13:59:21 +01:00
parent 5edeecd126
commit a4d072ae17
No known key found for this signature in database
GPG Key ID: F423E37A2415E160
1 changed files with 32 additions and 0 deletions

32
Utils/Extensions.cs Normal file
View File

@ -0,0 +1,32 @@
namespace AdventOfCode.Utils;
public static class Extensions
{
/// <summary>
/// Returns a slice of the given list
/// </summary>
/// <param name="list">The list you pretend to slice</param>
/// <param name="startIndex">The first index of the slice</param>
/// <param name="endIndex">The index after the last index of the slice</param>
/// <returns></returns>
public static List<T> Sublist<T>(this List<T> list, int startIndex, int endIndex)
{
List<T> result = new List<T>();
for (int i = startIndex; i < endIndex; i++)
result.Add(list[i]);
return result;
}
/// <summary>
/// Returns a column of a give matrix list
/// </summary>
/// <param name="list">2 dimensions list</param>
/// <param name="column">Column index</param>
/// <returns></returns>
public static List<T> GetColumn<T>(this List<List<T>> list, int column)
{
List<T> result = new List<T>();
foreach (List<T> row in list) result.AddRange(row.Where((t, i) => i == column));
return result;
}
}