From 6f4c1c0685147d2f1b6fd6afa7d4a590f331a7bb Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Sun, 11 Jun 2023 19:36:02 +0100 Subject: [PATCH] [2022] Utils update --- Utils/{ => Extensions}/LinqExtensions.cs | 6 ++- Utils/Extensions/MathExtensions.cs | 58 ++++++++++++++++++++++++ Utils/MathExtensions.cs | 14 ------ Utils/MathTools.cs | 11 +++++ Year2022/Day5.cs | 2 +- Year2022/Day8.cs | 2 +- Year2022/Day9.cs | 2 +- 7 files changed, 76 insertions(+), 19 deletions(-) rename Utils/{ => Extensions}/LinqExtensions.cs (95%) create mode 100644 Utils/Extensions/MathExtensions.cs delete mode 100644 Utils/MathExtensions.cs create mode 100644 Utils/MathTools.cs diff --git a/Utils/LinqExtensions.cs b/Utils/Extensions/LinqExtensions.cs similarity index 95% rename from Utils/LinqExtensions.cs rename to Utils/Extensions/LinqExtensions.cs index f417bf2..39c10ef 100644 --- a/Utils/LinqExtensions.cs +++ b/Utils/Extensions/LinqExtensions.cs @@ -1,4 +1,4 @@ -namespace AdventOfCode.Utils; +namespace AdventOfCode.Utils.Extensions; public static class LinqExtensions { @@ -9,9 +9,11 @@ public static class LinqExtensions /// The first index of the slice /// The index after the last index of the slice /// - public static List Sublist(this List list, int startIndex, int endIndex) + public static List Sublist(this List list, int startIndex, int endIndex = default) { List result = new List(); + if (endIndex == default) + endIndex = list.Count; for (int i = startIndex; i < endIndex; i++) result.Add(list[i]); return result; diff --git a/Utils/Extensions/MathExtensions.cs b/Utils/Extensions/MathExtensions.cs new file mode 100644 index 0000000..384587d --- /dev/null +++ b/Utils/Extensions/MathExtensions.cs @@ -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 list, int maxCount) + { + List maxList = new List(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 list) + { + int lcm = 1; + foreach (int i in list) + lcm *= i / MathTools.GreatCommonDivider(lcm, i); + return lcm; + } +} \ No newline at end of file diff --git a/Utils/MathExtensions.cs b/Utils/MathExtensions.cs deleted file mode 100644 index 7785629..0000000 --- a/Utils/MathExtensions.cs +++ /dev/null @@ -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 - }; - } -} \ No newline at end of file diff --git a/Utils/MathTools.cs b/Utils/MathTools.cs new file mode 100644 index 0000000..b37b4fd --- /dev/null +++ b/Utils/MathTools.cs @@ -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); + } +} \ No newline at end of file diff --git a/Year2022/Day5.cs b/Year2022/Day5.cs index 080550f..1842252 100644 --- a/Year2022/Day5.cs +++ b/Year2022/Day5.cs @@ -1,4 +1,4 @@ -using AdventOfCode.Utils; +using AdventOfCode.Utils.Extensions; namespace AdventOfCode.Year2022; diff --git a/Year2022/Day8.cs b/Year2022/Day8.cs index 34d467b..e0a3cce 100644 --- a/Year2022/Day8.cs +++ b/Year2022/Day8.cs @@ -1,4 +1,4 @@ -using AdventOfCode.Utils; +using AdventOfCode.Utils.Extensions; namespace AdventOfCode.Year2022; diff --git a/Year2022/Day9.cs b/Year2022/Day9.cs index dcf8849..33d6642 100644 --- a/Year2022/Day9.cs +++ b/Year2022/Day9.cs @@ -1,4 +1,4 @@ -using AdventOfCode.Utils; +using AdventOfCode.Utils.Extensions; namespace AdventOfCode.Year2022;