Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Solving 24 puzzles

by MeowChow (Vicar)
on May 21, 2001 at 01:51 UTC ( #81872=note: print w/ replies, xml ) Need Help??


in reply to Solving 24 puzzles

This is a great puzzle which spectacularly combines two previous golf outings. I took the route of least resistance -- instead of bothering with parenthesis, I used reverse-polish notation expressions. This significantly simplified things:

sub c {@r='';@r=map{$c=$_;map"$c $_",@r}@_ for 1..shift;@r} sub puzzle { ($x,$q,@l)=@_;@l=c$q,@l;@o=c$q-1,qw'+ - / *'; map{$n=$_;map{$t=$n.$_;{$t=~s!(\S+)\s+(\S+)\s*([-+*/])!"$1$3 $2"!ee& +&redo}; $t eq"$x "?$n.$_:()}@o}@l } ## example usage: puzzle ($total, $size, @numbers) ## print join $/, puzzle(24, 4, 6, 9); 6 6 6 6 + + + 6 6 6 9 / * * 6 6 9 6 - * + 6 6 9 6 / / * 6 9 6 6 * / /
That code is somewhat golfed, and damn messy, so I'm sure it can be significantly improved upon.
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print


Comment on Re: Solving 24 puzzles
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://81872]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-07-30 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls