/// <summary> /// Computes each unique set of values of length SquareRoot(values.Length) that can be /// formed from the given values. /// </summary> /// <param name="values"> /// All the possible values for the magic square. Each value must be unique. /// </param> public static HashSet <BitVector> ComputeSets(ReadOnlySpan <int> values) { int boxSize = Boxes.IntSquareRoot(values.Length); BitVector allPossibleValues = new BitVector(); for (int i = 0; i < values.Length; ++i) { if (allPossibleValues.IsBitSet(values[i])) { throw new ArgumentException("Values must be unique."); } allPossibleValues.SetBit(values[i]); } return(ComputeSets(values, boxSize, allPossibleValues)); }
internal static string ToString(IReadOnlyPuzzle puzzle) { int size = puzzle.Size; bool showBoxes = Boxes.TryIntSquareRoot(size, out int boxSize); if (showBoxes) { return(_ToStringWithBoxes(puzzle, boxSize)); } int maxDigit = puzzle.AllPossibleValuesSpan.FindMax(); var shape = new PuzzleShape { Size = size, SquareWidth = maxDigit.ToString(NumberFormatInfo.InvariantInfo).Length }; var normalChars = _GetCharsForNormalRow(); var strBuild = new StringBuilder(); for (int row = 0; row < size; row++) { _AppendDividerRow(shape, _GetCharsForDivider(row, shape), strBuild); strBuild.Append(normalChars.RowStart); for (int col = 0; col < size; col++) { _AppendSquareContents(puzzle[row, col], shape, strBuild); if (col == size - 1) { strBuild.Append(normalChars.RowEnd); } else { strBuild.Append(normalChars.BetweenColumns); } } strBuild.Append('\n'); } _AppendDividerRow(shape, _GetCharsForDivider(size, shape), strBuild); return(strBuild.ToString()); }
/// <summary> /// Computes the "sum" that each row/column/diagonal must add up to. /// </summary> /// <param name="values"> /// All the possible values for the magic square. Each value must be unique. /// </param>. /// <exception cref="ArgumentException"> /// Thrown if no sum is possible. /// </exception> public static int ComputeSum(ReadOnlySpan <int> values) { int boxSize = Boxes.IntSquareRoot(values.Length); return(_ComputeSum(values, boxSize)); }