private static bool allTestsPassed = true;
public static void Main()
Console.WriteLine($"allTestsPassed: {allTestsPassed}");
public static (int leftTakeCount, int rightTakeCount) GetTakeCountsFromTwoSourcesToMeetOneTotalQuota(int take, int leftCount, int rightCount)
int totalCount = leftCount + rightCount;
bool totalCountAboveTake = totalCount > take;
bool leftAboveHalfTake = leftCount > halfTake;
bool shouldReduceLeft = totalCountAboveTake && leftAboveHalfTake;
bool rightAboveHalfTake = rightCount > halfTake;
bool shouldReduceRight = totalCountAboveTake && rightAboveHalfTake;
bool canReduceBothToHalftake = totalCountAboveTake & leftAboveHalfTake & rightAboveHalfTake;
int leftReductionToHalfTake = canReduceBothToHalftake ? leftCount - halfTake : 0;
int rightReductionToHalfTake = canReduceBothToHalftake ? rightCount - halfTake : 0;
int leftReductionToDifference = (shouldReduceLeft & !canReduceBothToHalftake) ? leftCount - (take - rightCount) : 0;
int rightReductionToDifference = (shouldReduceRight & !canReduceBothToHalftake) ? rightCount - (take - leftCount) : 0;
int maxLeftReduction = Math.Max(leftReductionToHalfTake, leftReductionToDifference);
int maxRightReduction = Math.Max(rightReductionToHalfTake, rightReductionToDifference);
int finalLeftReduction = shouldReduceLeft ? maxLeftReduction : 0;
int finalRightReduction = shouldReduceRight ? maxRightReduction : 0;
int leftTakeCount = leftCount - finalLeftReduction;
int rightTakeCount = rightCount - finalRightReduction;
return (leftTakeCount, rightTakeCount);
private static void TestCartisian()
var cartisianRunUntil = 20;
var ultimateTakeLimit = 10;
for(int left = 0; left < runUntil; left++)
expectedCount = Math.Min(ultimateTakeLimit, left);
TestLine(ultimateTakeLimit, left, 0, expectedCount);
for(int right = 0; right < runUntil; right++)
expectedCount = Math.Min(ultimateTakeLimit, right);
TestLine(ultimateTakeLimit, 0, right, expectedCount);
for(int left = 0; left < cartisianRunUntil; left++)
for(int right = 0; right < cartisianRunUntil; right++)
expectedCount = Math.Min(ultimateTakeLimit, left+right);
TestLine(ultimateTakeLimit, left, right, expectedCount);
private static void TestLine(int finalTakeCount, int leftDataSetCount, int rightDataSetCount, int expectedCount)
var takeCounts = GetTakeCountsFromTwoSourcesToMeetOneTotalQuota(finalTakeCount, leftDataSetCount, rightDataSetCount);
int resultCount = takeCounts.leftTakeCount + takeCounts.rightTakeCount;
bool passes = resultCount == expectedCount;
allTestsPassed = allTestsPassed && passes;
Console.WriteLine($"leftDataSet: {leftDataSetCount}.Take({takeCounts.leftTakeCount}) rightDataSet: {rightDataSetCount}.Take({takeCounts.rightTakeCount}) resultCount: {resultCount} expectedCount: {expectedCount} passes: {passes}\r\n");