P is for Practical PerlMonks

### Re: Behold! The power of recursion.

by Jasper (Chaplain)
 on Oct 19, 2004 at 11:50 UTC ( #400479=note: print w/ replies, xml ) Need Help??

in reply to Behold! The power of recursion.

Almost all my solutions to various maze-like perlgolf competitions have been recursive. For example, the rush-hour problem, where given a state of cars on a 'road', move them around until you can get a particular car out of the jam.
```sub g{my(\$b,\$r)=@_;\$s{\$b}++||map{\$p='.'x8x/[A-Z]/;\$s||=\$r x\$b=~/\$c
/||g(\$n=\$b,"\$r\$c ".(\$n=~s/\$_((\$p\$c)+\$p)(?!\$_)(\$c| )/\$3\$1\$_/s-2*/ /).\$/
+)for\$",\$c=\$_}\$b=~/\w/g;\$s}\$_=g\$_
Look here for the rules, and links to the other solutions.

I can't think of a way to solve this sort of thing iteratively, unless you're willing/planning to take forever. And I think the solutions for that problem illustrate the terseness available to recursion to solve quite an awkward problem. (my solution was 170-odd characters - the winning solution under 140!). And my 500MHz machine still solved the problems in a reasonable length of time.

Also, isn't the regular expression engine recursive?

Create A New User
Node Status?
node history
Node Type: note [id://400479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2016-07-29 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (264 votes). Check out past polls.