[2022] Utils update

This commit is contained in:
Tiago Garcia 2023-06-11 19:36:02 +01:00
parent cff59d5939
commit 6f4c1c0685
No known key found for this signature in database
GPG Key ID: F423E37A2415E160
7 changed files with 76 additions and 19 deletions

View File

@ -1,4 +1,4 @@
namespace AdventOfCode.Utils; namespace AdventOfCode.Utils.Extensions;
public static class LinqExtensions public static class LinqExtensions
{ {
@ -9,9 +9,11 @@ public static class LinqExtensions
/// <param name="startIndex">The first index of the 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> /// <param name="endIndex">The index after the last index of the slice</param>
/// <returns></returns> /// <returns></returns>
public static List<T> Sublist<T>(this List<T> list, int startIndex, int endIndex) public static List<T> Sublist<T>(this List<T> list, int startIndex, int endIndex = default)
{ {
List<T> result = new List<T>(); List<T> result = new List<T>();
if (endIndex == default)
endIndex = list.Count;
for (int i = startIndex; i < endIndex; i++) for (int i = startIndex; i < endIndex; i++)
result.Add(list[i]); result.Add(list[i]);
return result; return result;

View File

@ -0,0 +1,58 @@
namespace AdventOfCode.Utils.Extensions;
public static class MathExtensions
{
public static int Signal(this int x)
{
return x switch
{
< 0 => -1,
> 0 => 1,
_ => 0
};
}
public static bool IsDigit(this string s)
{
try
{
Convert.ToInt32(s);
}
catch (FormatException ignored)
{
return false;
}
return true;
}
public static ulong ProductOfMax(this List<ulong> list, int maxCount)
{
List<ulong> maxList = new List<ulong>(maxCount);
foreach (ulong number in list)
{
if (maxList.Count < maxList.Capacity)
maxList.Add(number);
else
if (number > maxList.Min())
{
maxList.RemoveAt(0);
maxList.Add(number);
}
maxList.Sort();
}
ulong product = 1;
maxList.ForEach(n => product*=n);
return product;
}
public static int LeastCommonMultiplier(this List<int> list)
{
int lcm = 1;
foreach (int i in list)
lcm *= i / MathTools.GreatCommonDivider(lcm, i);
return lcm;
}
}

View File

@ -1,14 +0,0 @@
namespace AdventOfCode.Utils;
public static class MathExtensions
{
public static int Signal(this int x)
{
return x switch
{
< 0 => -1,
> 0 => 1,
_ => 0
};
}
}

11
Utils/MathTools.cs Normal file
View File

@ -0,0 +1,11 @@
namespace AdventOfCode.Utils;
public static class MathTools
{
public static int GreatCommonDivider(int x, int y)
{
if (x % y == 0)
return y;
return GreatCommonDivider(y, x % y);
}
}

View File

@ -1,4 +1,4 @@
using AdventOfCode.Utils; using AdventOfCode.Utils.Extensions;
namespace AdventOfCode.Year2022; namespace AdventOfCode.Year2022;

View File

@ -1,4 +1,4 @@
using AdventOfCode.Utils; using AdventOfCode.Utils.Extensions;
namespace AdventOfCode.Year2022; namespace AdventOfCode.Year2022;

View File

@ -1,4 +1,4 @@
using AdventOfCode.Utils; using AdventOfCode.Utils.Extensions;
namespace AdventOfCode.Year2022; namespace AdventOfCode.Year2022;