Sim Carol 2001, including this document, was written by Bill Godfrey.
| Look at an example diagram |
SimCarol 2001 uses a brute force recursive descent algorithm. All possibilities are tried, inching towards one which matches the solution.
At each step, a list of available numbers are maintained. At the start, this list comes from the numbers given at the command line.
For each pairing of numbers, each of the four operators are applied.
In the case of subtraction and division, it is always the highest value (subtracted or divided by) the lower value. The rules do not allow a step to use negative or fractional values, and so the problem of "non-communative" operations do not apply, as there is only one valid way to apply any two numbers.
So for each triplet of two numbers and an operator, the result of this one calculation is found. If it matches the target, or gets closer than the previous near match, it is stored.
With the result of this one calculation, the pairing function is recursively called, this time with the two chosen numbers replaced by the one result of the previous calculation.
The recursion continues until the starting numbers have been reduced to one number. The recursion then backtracks to the caller and the next triplet of two numbers and an operator is attempted.
Similarly, when all triplets of two numbers and an operator have been exhasted, the recursion backracks to the caller.
When an exact match is found, the result is displayed. Any subsequent matches discovered are only displayed if the new result takes less steps than the previous discovery.
At the end, if no exact match is found, the nearest match is displayed.
Main's job is to parse the command line, validate and to start the recursion process.
Additionally, main will report the nearest match, if an exact match could not be found.
This is the recursive core of the program. At the core are three nested for loops, generating triplets of two numbers and an operator.
Before it is processed, each triplet is validated.
A complete copy is made of the previous state, so that a recurrsive back track will be possible. This new copy will have the triplet applied, so that the two selected terms are replaced the one result of the calculation. (This update process is preformed by the update_unused function.)
This new possible solution is checked by the evaluate_sol function to see if it is a match, or a better match than the previous nearest find.
Finally, go_calc is recurrsively called, passing in the updated copy of the solution under development.
SimCarol 2001 was written by Bill Godfrey, with a bit of inspiration from Dave Jackson.