From 93f5571bbb6deca8a875509f0e8c100b9d655258 Mon Sep 17 00:00:00 2001 From: AJ Date: Sun, 11 Dec 2022 21:42:16 -0500 Subject: [PATCH] BestCombination --- SawCut/BestCombination.cs | 104 ++++++++++++++++++++++++++++++++++++++ SawCut/SawCut.csproj | 2 + SawCut/Tolerance.cs | 14 +++++ 3 files changed, 120 insertions(+) create mode 100644 SawCut/BestCombination.cs create mode 100644 SawCut/Tolerance.cs diff --git a/SawCut/BestCombination.cs b/SawCut/BestCombination.cs new file mode 100644 index 0000000..04391a5 --- /dev/null +++ b/SawCut/BestCombination.cs @@ -0,0 +1,104 @@ +using System; + +namespace SawCut +{ + public static class BestCombination + { + public static BestComboResult FindFrom2(double length1, double length2, double stockLength) + { + var result = new BestComboResult(); + result.Item1Length = length1; + result.Item2Length = length2; + + stockLength += Tolerance.Epsilon; + + if (length1 > stockLength) + { + if (length2 > stockLength) + { + //throw new Exception($"Either {nameof(length1)} or {nameof(length2)} must be less than or equal to the {nameof(stockLength)}"); + return null; + } + + result.Item1Count = 0; + result.Item2Count = (int)Math.Floor(stockLength / length2); + return result; + } + + if (length2 > stockLength) + { + result.Item1Count = (int)Math.Floor(stockLength / length1); + result.Item2Count = 0; + return result; + } + + var maxCountLength1 = (int)Math.Floor(stockLength / length1); + + result.Item1Count = maxCountLength1; + result.Item2Count = 0; + + var remnant = stockLength - maxCountLength1 * length1; + + if (remnant.IsEqualTo(0)) + return result; + + for (int countLength1 = 0; countLength1 <= maxCountLength1; ++countLength1) + { + var remnant1 = stockLength - countLength1 * length1; + + if (remnant1 >= length2) + { + var countLength2 = (int)Math.Floor(remnant1 / length2); + var remnant2 = remnant1 - length2 * countLength2; + + if (!(remnant2 < remnant)) + continue; + + result.Item1Count = countLength1; + result.Item2Count = countLength2; + + if (remnant2.IsEqualTo(0)) + break; + + remnant = remnant2; + } + else + { + if (!(remnant1 < remnant)) + continue; + + result.Item1Count = countLength1; + result.Item2Count = 0; + + if (remnant1.IsEqualTo(0)) + break; + + remnant = remnant1; + } + } + + return result; + } + } + + public class BestComboResult + { + public double Item1Length { get; set; } + + public int Item1Count { get; set; } + + public double Item1TotalLength + { + get { return Item1Length * Item1Count; } + } + + public double Item2Length { get; set; } + + public int Item2Count { get; set; } + + public double Item2TotalLength + { + get { return Item2Length * Item2Count; } + } + } +} \ No newline at end of file diff --git a/SawCut/SawCut.csproj b/SawCut/SawCut.csproj index 96a96fe..009a0c1 100644 --- a/SawCut/SawCut.csproj +++ b/SawCut/SawCut.csproj @@ -42,6 +42,7 @@ + @@ -52,6 +53,7 @@ + \ No newline at end of file diff --git a/SawCut/Tolerance.cs b/SawCut/Tolerance.cs new file mode 100644 index 0000000..2fdf874 --- /dev/null +++ b/SawCut/Tolerance.cs @@ -0,0 +1,14 @@ +using System; + +namespace SawCut +{ + public static class Tolerance + { + public const double Epsilon = 0.00001; + + public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon) + { + return Math.Abs(b - a) <= tolerance; + } + } +} \ No newline at end of file