Come for the quick hacks, stay for the epiphanies. PerlMonks

### comment on

 Need Help??
At first I had ignored this, then decided to do it. It was a more fun challenge than I thought. There are, not counting the order of the moves, actually 4 solutions in 15 moves for a 5x5 board. What follows is the throw-away script I wrote to find this. By default it solves a 5x5 board. Pass it an argument and it will solve an nxn board. (I tried it in the 1..10 range and found that there is 1 solution for 1, 2, 3, 6, 7, 8 and 10. As I mentioned, there are 4 for 5, plus 16 for 4 and 256 for 9. Don't ask me why, I merely report what I found...)

It would not be hard to extend this to handle arbitrary rectangular boards. I also didn't need the globals but this is throw-away code and it was easier that way. I make no apologies for the huge numbers of anonymous functions. The fact that I can feasibly find all 64 solutions for an 11x11 board by brute-force search on my old laptop speaks loudly enough for the efficiency of the method...

```use strict;
use Carp;
use vars qw(\$min \$max @board @soln @toggles);
\$min = 1;
\$max = shift(@ARGV) || 5;
@board = map [map 0, \$min..\$max], \$min..\$max;
foreach my \$x (\$min..\$max) {
foreach my \$y (\$min..\$max) {
push @toggles, ["\$x-\$y", ret_toggle_square(\$x, \$y)];
}
}

find_soln();

sub find_soln {
if (! @toggles) {
# Solved!
print join " ", "Solution:", map \$_->[0], @soln;
print "\n";
}
else {
my \$toggle = shift(@toggles);
# Try with, then without
if (\$toggle->[1]->()) {
push @soln, \$toggle;
find_soln();
pop @soln;
}
if (\$toggle->[1]->()) {
find_soln();
}
unshift @toggles, \$toggle;
}
}

# Returns a function that switches one square and returns
# true iff the new color is black
sub ret_swap_square {
my (\$x, \$y) = @_;
#print "Generated with \$x, \$y\n";
my \$s_ref = \(\$board[\$x-1][\$y-1]);
return sub {\$\$s_ref = (\$\$s_ref + 1) %2;};
}

# Returns a function that toggles one square and its
# neighbours, and returns whether or not any neighbour
# swapping again with \$x lower or \$x the same and \$y lower.
sub ret_toggle_square {
my (\$x, \$y) = @_;
my @fin_swaps;
my @other_swaps;
unless (\$x == \$min) {
push @fin_swaps, ret_swap_square(\$x - 1, \$y);
}
if (\$x == \$max) {
unless (\$y == \$min) {
push @fin_swaps, ret_swap_square(\$x, \$y - 1);
}
if (\$y == \$max) {
push @fin_swaps, ret_swap_square(\$x, \$y);
}
else {
push @other_swaps, ret_swap_square(\$x, \$y);
unless (\$y == \$max) {
push @other_swaps, ret_swap_square(\$x, \$y+1);
}
}
}
else {
unless (\$y == \$min) {
push @other_swaps, ret_swap_square(\$x, \$y - 1);
}
push @other_swaps, ret_swap_square(\$x, \$y);
push @other_swaps, ret_swap_square(\$x + 1, \$y);
unless (\$y == \$max) {
push @other_swaps, ret_swap_square(\$x, \$y + 1);
}
}
return sub {
\$_->() foreach @other_swaps;
my \$ret = 1;
\$ret *= \$_->() foreach @fin_swaps;
return \$ret;
}
}

In reply to Re (tilly) 1: 5x5 Puzzle by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-06-19 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.