Skip to content

Instantly share code, notes, and snippets.

@chadman
Created November 7, 2015 04:17
Show Gist options
  • Save chadman/1180f830fd4a2392c4e8 to your computer and use it in GitHub Desktop.
Save chadman/1180f830fd4a2392c4e8 to your computer and use it in GitHub Desktop.
Given a specific amount of money on hand, calculate the CC bills to pay off to save the most amount of money a month
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace budget {
public class Program {
static void Main(string[] args) {
var debt = new DebtCalculation(500);
debt.RunBudget();
Console.ReadKey();
}
}
public class DebtCalculation {
public DebtCalculation(decimal amountOnHand) {
// Add as many items that you want
_budgetItems.Add(new BudgetItem { Name = "Credit Card Name", MonthlyCost = 2, TotalRemaining = 100 });
_budgetItems.Add(new BudgetItem { Name = "toys R Us", MonthlyCost = 25, TotalRemaining = 1000 });
_budgetItems.Add(new BudgetItem { Name = "Bank of America", MonthlyCost = 278, TotalRemaining = 2500 });
_budgetItems.Add(new BudgetItem { Name = "First Bank", MonthlyCost = 23, TotalRemaining = 100 });
_amountOnHand = amountOnHand;
}
private List<BudgetItem> _budgetItems = new List<BudgetItem>();
private decimal _amountOnHand = 0;
public void RunBudget() {
decimal greatestSavings = 0;
foreach (BudgetItem[] permutation in Permutation.GetPermutations(_budgetItems.ToArray(), _amountOnHand)) {
decimal amountTotal = 0;
decimal amountOwed = 0;
permutation.ToList().ForEach(x => {
if (x != null) {
amountTotal += x.MonthlyCost;
amountOwed += x.TotalRemaining;
}
});
if (greatestSavings < amountTotal) {
List<string> names = new List<string>();
permutation.ToList().ForEach(x => {
if (x != null) {
names.Add(x.Name);
}
});
Console.WriteLine(String.Join(", ", names));
Console.WriteLine(amountTotal + " " + amountOwed);
greatestSavings = amountTotal;
}
}
//for (int i = 0; i < _budgetItems.Count; i++) {
// var totalCalculated = 0;
// GetP
//}
}
}
public class BudgetItem {
public string Name { get; set; }
public decimal MonthlyCost { get; set; }
public decimal TotalRemaining { get; set; }
}
public class Permutation {
public static IEnumerable<BudgetItem[]> GetPermutations(BudgetItem[] items, decimal amountOnHand) {
int[] work = new int[items.Length];
for (int i = 0; i < work.Length; i++) {
work[i] = i;
}
foreach (int[] index in GetIntPermutations(work, 0, work.Length)) {
BudgetItem[] result = new BudgetItem[index.Length];
var count = 0;
for (int i = 0; i < index.Length; i++) {
if (count == 0) {
result[count] = items[index[i]];
count++;
continue;
}
decimal amountTotal = 0;
result.ToList().ForEach(x => {
if (x != null) {
amountTotal += x.TotalRemaining;
}
});
if (amountTotal + items[index[i]].TotalRemaining < amountOnHand) {
result[count] = items[index[i]];
count++;
}
}
yield return result;
}
}
public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len) {
if (len == 1) {
yield return index;
}
else if (len == 2) {
yield return index;
Swap(index, offset, offset + 1);
yield return index;
Swap(index, offset, offset + 1);
}
else {
foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1)) {
yield return result;
}
for (int i = 1; i < len; i++) {
Swap(index, offset, offset + i);
foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1)) {
yield return result;
}
Swap(index, offset, offset + i);
}
}
}
private static void Swap(int[] index, int offset1, int offset2) {
int temp = index[offset1];
index[offset1] = index[offset2];
index[offset2] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment