Welcome to the Monastery  
PerlMonks 
Solving 24 puzzlesby tigervamp (Friar) 
on May 20, 2001 at 21:36 UTC ( #81856=perlquestion: print w/ replies, xml )  Need Help?? 
tigervamp has asked for the
wisdom of the Perl Monks concerning the following question:
Out of extreme boredom, I wrote the following code to solve 24 puzzles. For those of you that have never played the game 24, you must make the number 24 out of 4 other nonzero numbers using the basic 4 arithmetic operations. For example, the numbers 5, 5, 8, 8 can be made to equal 24 bacause 5*58/8=24. With that in mind, the following code solves 24 puzzles by basically trying every possible combination of operations on the four numbers (I think), and then displaying the unique solutions or an error message if no solutions are found. I use a regular expression to introduce parentheses into the expressions to be evaluated to account for solutions that should be evaluated against the order of operations. My two questions are:
1. As you can see in the beginning of my program, I use an array called @optimize_me to store the different orders that numbers will be evaluated. I want to be able to easily convert this program to solve 5, 6, or more numbers. Is there an easier way to to go about this besides the array method I used? Maybe a way to dynamically create such an array? 2. The other problem with solving with more than 4 numbers is that the regular expression that inserts parantheses quickly gets unwieldly. Is the a better way to go about this step or a way to create a regular esxpression that will work for all cases? Here is my code:
Any help or comments of any kind are greatly appreciated. Thanks for your time, tigervamp
Back to
Seekers of Perl Wisdom

