public bool IsValid(NumberChain numberChain) { try { // Only seven numbers if (numberChain.Length != 7) return false; var operators = Operators.Matches(numberChain.ToString()).Cast<object>().Select(x => x.ToString()).ToList(); // Only 6 operators if (operators.Count != 6) return false; // All 4 operators should be used var grouped = operators.GroupBy(x => x); if (grouped.Count() != 4) return false; // Maximum of 2 operators can be used twice var groupedByCount = grouped.GroupBy(x => x.Count()); var countOfOperatorsUsedTwice = groupedByCount.FirstOrDefault(x => x.Key == 2)?.Count(); if (countOfOperatorsUsedTwice != 2) return false; // Validated return true; } catch { return false; } }
public static bool IsValidOn(this ArithmeticOperation operation, NumberChain left, byte right) { return operation.IsValidOn(left.Result ?? left.Number, right); }
public IEnumerable<NumberChain> GenerateChains(byte number, NumberChain previous) { return GetPossibleOperations() .Where(x => x.IsValidOn(previous, number)) .Select(x => new NumberChain(number, previous, x)); }