protected bool CheckAddSolution(CountdownNumber number) { bool match = number.Value == puzzle.solution; if (match) { solutions.Add(number); } return(match); }
/// <summary> /// Tries every group against every other group /// </summary> private void CombineAll() { var hashes = container.GetHashes(); //do I need to copy? var count = hashes.Count; for (int first = 0; first < count; first++) { for (int second = first + 1; second < count; second++) { if (CountdownNumber.CanCombine(hashes[first], hashes[second])) { CombineTwoSets(hashes[first], hashes[second]); } } } }
private void CombineTwoSets(int firstHash, int secondHash) { Debug.Assert(CountdownNumber.CanCombine(firstHash, secondHash)); var firstList = container.GetNumbers(firstHash); var secondList = container.GetNumbers(secondHash); //pre-allocate results list var results = new List <CountdownNumber>(3 * firstList.Count * secondList.Count); //3 is the magic number (of operations) foreach (var n1 in firstList) { foreach (var n2 in secondList) { foreach (var op in Enum.GetValues(typeof(ComboCountdownNumber.Operation)).Cast <ComboCountdownNumber.Operation>()) { results.Add(new ComboCountdownNumber(n1, n2, op)); } } } }
public ComboCountdownNumber(CountdownNumber parentA, CountdownNumber parentB, Operation operation) { if (operation == Operation.SUBTRACT && parentA.Value < parentB.Value) { //special case to keep all numbers positive this.parentA = parentB; this.parentB = parentA; } else { //default don't change ordering. this.parentA = parentA; this.parentB = parentB; } //calculate the value and hash. this.operation = operation; this.Value = CalculateValue(); this.NumbersHash = parentA.CombineHash(ref parentB); Debug.Assert(this.Value >= 0); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns>the combined hash of the two numbers</returns> public int CombineHash(ref CountdownNumber other) { return(CombineHash(this.NumbersHash, other.NumbersHash)); }
/// <summary> /// Tests if the numbers can be combined i.e. the result won't use any duplicate numbers. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool CanCombine(ref CountdownNumber other) { return(CanCombine(this.NumbersHash, other.NumbersHash)); }