public Outcome DoTheCandySwap(Outcome outcome, out bool hasSwapped, int depth = 0) { if (depth == 0) { Console.WriteLine("---------- SWIP SWAP SWAP ---------"); } else { Console.WriteLine($" Swapping at depth {depth}"); } var swaps = FindGoodSwaps(outcome); if (swaps.Count == 0 || depth == Depth) { hasSwapped = false; return(outcome); } hasSwapped = true; var bestBag = outcome.Copy(); Console.WriteLine($"Found {swaps.Count()} swaps"); foreach (var swap in swaps) { var newBag = outcome.Copy(); newBag.Remove(swap.From, swap.Give); newBag.Add(swap.From, swap.Take); newBag.Remove(swap.To, swap.Take); newBag.Add(swap.To, swap.Give); bool tmp; newBag = DoTheCandySwap(newBag, out tmp, depth + 1); if (newBag.IsBetterThan(bestBag)) { Console.WriteLine($"Bag upgrade!"); bestBag = newBag; } } if (depth == 0) { for (int i = 0; i < Iterations; i++) { Console.WriteLine(" -- Here we go swapping again!! -- "); bool didSwap; bestBag = DoTheCandySwap(bestBag, out didSwap, 1); if (!didSwap) { break; } } } return(bestBag); }
public Outcome DoTheCandySteal(Outcome bags, out bool didSteel, int depth = 0) { if (depth == 0) { Console.WriteLine("---------- STEAL STEAL STEAL ---------"); } else { Console.WriteLine($" Stealing at depth {depth}"); } var swaps = FindGoodSteals(bags); if (swaps.Count == 0 || depth == Depth) { didSteel = false; return(bags); } didSteel = true; var bestBag = bags.Copy(); Console.WriteLine($"Found {swaps.Count()} steals"); foreach (var swap in swaps) { var newBag = bags.Copy(); newBag.Add(swap.From, swap.Take); newBag.Remove(swap.To, swap.Take); bool tmp; newBag = DoTheCandySteal(newBag, out tmp, depth + 1); if (newBag.IsBetterThan(bestBag)) { Console.WriteLine($"Bag upgrade!"); bestBag = newBag; } } if (depth == 0) { for (int i = 0; i < Iterations; i++) { Console.WriteLine(" -- Here we go stealing again!! -- "); bool didSwap; bestBag = DoTheCandySteal(bestBag, out didSwap, 1); if (!didSwap) { break; } } } return(bestBag); }
public List <Outcome> GetChainSwaps(Outcome outcome) { var bestOutcome = outcome.Copy(); var list = new List <Outcome>() { bestOutcome }; var swapList = FindAllSwaps(outcome, GiveType.A_B_C); swapList.AddRange(FindAllSwaps(outcome, GiveType.A_B)); swapList.AddRange(FindAllSwaps(outcome, GiveType.A_B_C_A)); swapList.AddRange(FindAllSwaps(outcome, GiveType.A_B_C_D)); swapList.AddRange(FindAllSwaps(outcome, GiveType.A_B_C_D_A)); foreach (var swaps in swapList.OrderByDescending(x => x.Sum(y => y.Value)).ToList()) { var newOutcome = outcome.Copy(); foreach (var swap in swaps) { newOutcome.Remove(swap.From, swap.Give); newOutcome.Add(swap.To, swap.Give); } /* * if (newOutcome.IsBetterThan(bestOutcome)) * { * Console.WriteLine(" - New best chainswap! -"); * bestOutcome = newOutcome; * }*/ list.Add(newOutcome); } return(list.OrderByDescending(x => x.Min).ThenByDescending(x => x.Average).Take(3).ToList()); }
public Outcome DoChainSwap(Outcome outcome, out bool changed, int depth = 0) { changed = false; var outcomes = GetChainSwaps(outcome); if (outcomes.Any()) { if (depth < Depth) { var deeper = new List <Outcome>(); foreach (var item in outcomes) { var result = DoChainSwap(item, out changed, depth + 1); if (changed || depth < Depth - 1) { deeper.Add(result); } } } changed = false; var bestOutcome = outcome.Copy(); foreach (var newOutcome in outcomes.OrderByDescending(x => x.Min)) { if (newOutcome.IsBetterThan(bestOutcome)) { Console.WriteLine($"- New best chainswap! Outcomes: {outcomes.Count()} - Depth: {depth} -"); bestOutcome = newOutcome; changed = true; } } return(bestOutcome); } return(outcome); }