« Agile Hardware Design? | Main | Handling Floating Point Numbers »
Wednesday
23Aug2006

Kakuro Combinations

Enough Sudoku for a while? Try Kakuro!

I’ve created a small helper table lately, listing what numbers are possible in what condition (dependent on the required sum and the number of fields). You may download it as a PDF:

I could have evaluated the allowed numbers by hand, but decided to write a small console code snippet in C# instead. It’s neither optimized nor well designed, but it works… *note to myself: use a functional language next time*

Call the method Kakuro(s,f) with the required sum in s and the number of fields in f, and it returns all possible combinations (only the ascending permutation for each) and the union set of numbers appearing in at least one combination (that’s the numbers in the helper table above)

The recursive backtracking method (with inline console output for simplicity):

static BitArray AppendSummand(int summandsLeft, int targetSum, BitArray used, int sum, int nextSummand)
{
   if(summandsLeft == 0)
   {
      if(targetSum == sum)
      {   // found valid solution
         PrintUsed(used, false);
         return used;
      }
      else // invalid solution A
         return new BitArray(9);
   }
   if(nextSummand >= 10) // invalid solution B
       return new BitArray(9);
       
   BitArray usedWith = new BitArray(used);
   usedWith[nextSummand-1] = true; // try with this number
   BitArray resultWithout = AppendSummand(summandsLeft, targetSum, used, sum, nextSummand+1);
   BitArray resultWith = AppendSummand(summandsLeft-1, targetSum, usedWith, sum+nextSummand, nextSummand+1);
   // combine results for the summary
   return resultWithout.Or(resultWith);
}

And the method to run it:

static void Kakuro(int sum, int fields)
{
   Console.WriteLine("==================================");
   Console.WriteLine("** Sum {0} in {1} cells",sum,fields);
   BitArray used = new BitArray(9);
   BitArray res = AppendSummand(fields,sum,used,0,1);
   Console.Write("** Superset: ");
   PrintUsed(res, true);
   Console.WriteLine();
}

Finally the print method used to print the numbers to the console:

static void PrintUsed(BitArray used, bool all)
{
   Console.ForegroundColor = ConsoleColor.Green;
   if(all)
      for(int i=0;i<used.Length;i++)
      {
         Console.ForegroundColor = used[ i ] ? ConsoleColor.Green : ConsoleColor.Red;
         Console.Write((i+1).ToString() + " ");
      }
   else
      for(int i=0;i<used.Length;i++)
         if(used[ i ])
            Console.Write((i+1).ToString() + " ");
   Console.ResetColor();
   Console.WriteLine();
}

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (2)

I've found another nice kakuro combinations table on the web, you might want to have a look at it, too:

http://www.kevinpluck.net/kakuro/KakuroCombinations.html
September 16, 2006 | Unregistered CommenterChristoph Rüegg
Bless you! The Kevin Pluck table is neither downloadable nor neatly printable in its web form. Thanks!
November 9, 2006 | Unregistered CommenterJen

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.