Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Just another Perl shrine
 
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 wandering the Monastery: (7)
As of 2014-04-18 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls