### 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```

Create A New User
Node Status?
node history
Node Type: note [id://81872]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-11-23 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (336 votes). Check out past polls.

Notices?