public void SolutionStupidSimple() { int find = 4; Container a = new Container('a', 3); Container b = new Container('b', 5); Assert.IsTrue(a.Capacity == 3); Assert.IsTrue(b.Capacity == 5); Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); //0/0 b.Fill(); Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); Assert.IsTrue(b.Units == 5); //0/5 a.Transfer(b); //3/2 Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); Assert.IsTrue(a.Units <= a.Capacity); a.Dump(); Assert.IsTrue(a.Units == 0); Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); //0/2 a.Transfer(b); Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); Assert.IsTrue(a.Units == 2, "Expected 2"); Assert.IsTrue(b.Units == 0, "Expected 0"); //2/0 b.Fill(); //2/5 a.Transfer(b); //3/4 Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity); Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity); Assert.IsTrue(b.Units == find); Trace.TraceInformation("You Win!"); }
private static void NextStep(Container a, Container b, int find, List<Tuple<string, int, int>> steps) { if (steps.Count >= maxSteps) { Trace.TraceWarning("Max attempts exception: {0} : {1} | {2}", find, a.Capacity, b.Capacity); throw new ApplicationException("Max attempts exceeded without a solution."); } Tuple<string, int, int> step; if (b.IsEmpty) { b.Fill(); step = Tuple.Create(string.Format("[Fill] Container [{0}]", b.Name), a.Units, b.Units); } else if (a.IsFull) { a.Dump(); step = Tuple.Create(string.Format("[Empty] Container [{0}]", a.Name), a.Units, b.Units); } else { a.Transfer(b); step = (Tuple.Create(string.Format("[Transfer] Container [{0}] to [{1}]", b.Name, a.Name), a.Units, b.Units)); } Trace.TraceInformation(step.Item1); Trace.TraceInformation("{0}|{1}", step.Item2, step.Item3); steps.Add(step); if (a.Units != find && b.Units != find) NextStep(a, b, find, steps); }
internal static int NextStep(Container a, Container b, int find, int counter) { int maxSteps = 100; if (counter >= maxSteps) { Trace.TraceWarning("Max attempts exception: {0} : {1} | {2}", find, a.Capacity, b.Capacity); throw new ApplicationException("Max attempts exceeded without a solution."); } if (b.Units == 0) { b.Fill(); } else if (a.Units == a.Capacity) { a.Dump(); } else { a.Transfer(b); } a.Report(); b.Report(); counter++; if (a.Units != find && b.Units != find) { counter = NextStep(a, b, find, counter); } return(counter); }
internal static int NextStep(Container a, Container b, int find, int counter) { int maxSteps = 100; if (counter >= maxSteps) { Trace.TraceWarning("Max attempts exception: {0} : {1} | {2}", find, a.Capacity, b.Capacity); throw new ApplicationException("Max attempts exceeded without a solution."); } if (b.Units == 0) b.Fill(); else if (a.Units == a.Capacity) a.Dump(); else a.Transfer(b); a.Report(); b.Report(); counter++; if (a.Units != find && b.Units != find) counter = NextStep(a, b, find, counter); return counter; }