Introduction
Countdown is a popular day time television game show on Channel 4 in the UK. One of the games is a number puzzle where a contestant picks 6 numbers and then has to build an equation from them that equals a randomly generated target during a 30 second countdown. If you haven't seen the program the rules are available here
This puzzle is an ideal candidate to be solved by a computer. It won't ever forget which numbers it's already used and it will always find the really obvious solution that's staring you right in the face but just cannot see.
This article documents the algorithm that I came up with. As with most algorithms, the trick is doing it quickly.
The UI
While not quite up to commercial standards it provides enough functionality to play the game and exercise the solver.
The Choose button generates tiles and the target values.
The Start Timer button starts a 30 second timer.
The Solve button starts the SolvingEngine
.
How the Solving Engine works
This uses a simple brute force approach where every possible equation is evaluated. The equations are constructed in postfix form which removes the need for parentheses and considerably simplifies the problem. A map defines how the postfix equations are constructed.
The solving engine Solve()
method is shown below:
public void Solve()
{
for (int k = 2; k <= tiles.Length; k++)
{
List<List<int>> map = postfixMap[k];
Combinations<int> combinations = new Combinations<int>(tiles, k);
foreach (List<int> combination in combinations)
{
Permutations<int> permutations = new Permutations<int>(combination);
foreach (List<int> permutation in permutations)
{
foreach (List<int> mapRow in map)
SolveRecursive(1, mapRow, 0, permutation, 0, 0);
}
}
}
}
As you can see, it loops choosing successive k combinations of the 6 tiles. Then for each combination, it then calculates all the permutations for that combination. Then for each permutation, it tries to solve the puzzle by creating and solving all possible postfix equations using the map.
The solving engine SolveRecursive()
method is the main work horse. The code is rather long winded due to functions being manually inlined, so I've shown the equivalent pseudo code:
SolveRecursive(recursion_depth)
{
stack = stacks[recursion_depth] ;
while (mapEntry > 0)
{
stack.push ;
mapEntry ;
}
left_digit = stack.pop ;
right_digit = stack.peek ;
foreach (operator in {*, +, , /})
{
result = evaluate (left_digit operator right_digit) ;
if (there are more map entries)
{
next_stack = stacks[recursion_depth + 1]
copy stack to next_stack
next_stack.poke = result ;
SolveRecursive(recursion_depth + 1)
}
else
{
if (result == target)
{
record equation ;
break ;
}
if (no targets found and result is close enough)
record closest equation;
}
}
}
The method implements a sequence of pushing zero or more tiles onto the stack before executing an operator. It then recurses executing another sequence. Each recursive call gets its own copy of the current stack so that when the recursion unwinds, the caller can simply continue with the next operator. The recursion ends when the map is completed.
Although it is a linear function conceptually it can be thought of as a tree. Each sequence splits into 4 branches, one for each operator, and for each recursion every branch further splits into 4 more branches. This repeats until the end of the map is reached when the final result is obtained for the 4 operators. After the first equation is executed, subsequent equations are evaluated by executing only one operator rather than the complete equation. The same principal applies for each node in the tree as the recursion unwinds. Tree structures and recursion are a thing of beauty.
Combinations and Permutations
The Combinations and Permutations collection classes are implemented using the common pattern where the class's enumerator provides the next combination or permutation on demand. To make the code reusable I've written them using generics.
Both classes use Donald Knuth documented algorithms to generate the data in lexicographic order. I think I can safely assume they are good solutions. The permutation algorithm is actually a port of the C++ standard template library function std::next_permutation()
.
The combination algorithm doesn't produce duplicates. In addition, I've modified the algorithm to filter out duplicate combinations produced when the source collection itself contains duplicates.
Postfix Map
Postfix equations are built up of a repeating sequence of pushing digits on to a stack followed by executing operators. All equations start by pushing 2 digits followed by executing 0 or 1 operator, then another digit followed by 0 to 2 operators etc. There is always be one less operator than digits and equations always end with an operator.
Consider the case for 4 tiles, there will be 5 map sub entries, one for each possible equation:
In the first column the O represents the position of operators within the equation. The code generates column two, a count of the operators in that position. It then converts these counts into a map entries where numbers greater than zero means push that number of digits onto the stack and zeros indicates that operators should be executed.
So how many equations are there?
It's the sum of the number of equations for each number of tiles k. If there are no duplicate tiles, then this is straight forward.
The last term is the product of the operators. So we have:
This gives a total of 33,665,400 equations. Note that k = 6 accounts for 92% of the total.
However, according to the rules of the game, there can be one or two pairs of duplicate tiles. The problem then starts with calculating the number of combinations. Google found this elegantly explained solution. Of course, some of the combinations produced would then also contain duplicates which in turn affects the number of permutations. I've left that problem for the mathematicians and just put some instrumentation into the code. The results are:
In practice, the number of equations fully evaluated will be considerably smaller due to the positive integer arithmetic rules of the game. If an equation fails one of the rules, it is discarded as soon as it fails.
In addition, I also discard any equation where an identity operation is performed, that is divide or multiple by one, since it will be a duplicate equation.
I also filter out duplicate equations by detecting if the operator is acting on tile values directly rather than any intermediately derived value. If the operator is commutative, that is addition and multiplication; I discard the equation if the left digit is greater than the right.
The final filter is that multiplication is the first operator evaluated. If the equation is complete and the result is less than the target, then the other operators are skipped.
Ultimately, it will be dependent on the value of the tiles chosen and the target as to how many equations are evaluated fully.
For typical sets of tiles and targets:
The solver was run on a Dell Inspiron 5578 with an I77500 processor (2.7 GHz) and 16 GB of memory running windows 10. Note that the timings were taken when the solver was started via the keyboard rather than using the mouse. Clicking took approximately 20ms longer, presumably due to an additional background process run by the system.
Conclusion
Is there any point to this? Not really. It was just a fun academic exercise.
The inspiration behind version 2.0 was to learn WPF and MVVM. This will also make the code base much easier to port to android using Xamarin. My next project.
Google and Stack Overflow have proved an invaluable resource when learning WPF, MVVM and xaml. I copied the answers to many questions, modifying the code to suit the needs at hand. While I like WPF and xaml, it is rather a steep learning curve. I now understand why MVVM is rightly so popular.
History
 2.0  Replace the winforms UI with WPF. Refactor the code to the MVVM design pattern. Update the solution to VS 2015. Move the unit test code in to a unit test project. Rewrite the unit tests and add new view model tests.
 1.3  Fix a bug found by CodeProject member WideBoyDixon. The solving engine failed to find solutions that required two tiles of the same value to be divided.

 1.2  The countdown team at Channel 4 kindly replied to my email confirming that the target range starts at 101 rather than 100 so a single tile solution is not possible.
 1.1  Move the timing code outside of the solving engine so that it's clear what is being measured. Complete the exception handling in the test code. The tests passed so unfortunately that area was neglected. Various small code refactoring. No bug fixes.
 1.0  Initial release
References
Countdown Program website
http://www.channel4.com/programmes/countdown[^]
Postfix equations also known as Reverse Polish Notation
http://en.wikipedia.org/wiki/Reverse_Polish_notation[^]
Introduction to Combinations and Permutations
http://www.mathsisfun.com/combinatorics/combinationspermutations.html[^]
Algorithm L in C++ by Thomas Guest
http://wordaligned.org/articles/nextpermutation#tocimplementation[^]
Algorithm L in python by Björn Edström
http://blog.bjrn.se/2008/04/lexicographicpermutationsusing.html[^]
Counting combinations containing duplicates
http://mathforum.org/library/drmath/view/56197.html[^]
Permutations, Combinations, and Variations using C# Generics by Adrian Akison
http://www.codeproject.com/Articles/26050/PermutationsCombinationsandVariationsusingCG[^]