### Re: Lights out puzzle (non-perl solution)

by ambrus (Abbot)
 on Nov 29, 2011 at 14:38 UTC ( #940636=note: print w/replies, xml ) Need Help??

in reply to Lights out puzzle

Here's a non-perl solution, with explanation.

For larger boards, this one is much faster than my program above. (You could try (29, 30, 2) or (39, 40, 2) as large examples where there's a unique button combination this program finds very quickly but that would take ages to found with the previous program.)

Replies are listed 'Best First'.
Re^2: Lights out puzzle (non-non-perl solution)
by salva (Abbot) on Dec 01, 2011 at 18:33 UTC
Now solving a linear equation over GF(2) is something apparently very few libraries can do

That's quite easy to implement, so easy that, well... see Algorithm::GaussianElimination::GF2.

Using that module, the Lights On problem gets reduced to:

```use strict;
use warnings;

use Algorithm::GaussianElimination::GF2;

use 5.010;

(@ARGV >= 1 and @ARGV <= 2) or die "Usage:\n  \$0 len [width]\n\n";

my (\$len, \$w) = @ARGV;

unless (defined \$w) {
\$w = int sqrt(\$len);
\$w++ unless \$w * \$w == \$len;
}

my \$a = Algorithm::GaussianElimination::GF2->new;

for my \$ix (0..\$len-1) {
my \$eq = \$a->new_equation;

\$eq->b(1);
\$eq->a(\$ix, 1);
my \$up = \$ix - \$w;
\$eq->a(\$up, 1) if \$up >= 0;
my \$down = \$ix + \$w;
\$eq->a(\$down, 1) if \$down < \$len;
my \$left = \$ix - 1;
\$eq->a(\$left, 1) if \$left % \$w + 1 != \$w;
my \$right = \$ix + 1;
\$eq->a(\$right, 1) if \$right % \$w and \$right < \$len;
}

my (\$sol, @base0) = \$a->solve;

if (\$sol) {
my @sol = @\$sol;
while (@sol) {
my @row = splice @sol, 0, \$w;
say "@row";
}

for my \$sol0 (@base0) {
say "sol0:";
my @sol0 = @\$sol0;
while (@sol0) {
my @row = splice @sol0, 0, \$w;
say "@row";
}
}
}
else {
say "no solution found"
}
On my computer the 14x14-7 problem gets solved in 0.06 seconds.

Create A New User
Node Status?
node history
Node Type: note [id://940636]
help
Chatterbox?
 [LanX]: any news from tye ' s search? [MidLifeXis]: Ugh - who else? [Discipulus]: so the right term is 'gired' [Discipulus]: look at ot by karl Discipulus gired: adj the work position that stands between hired and fired.. LanX recommends inner immigration, work less hard and enjoy pay roll [MidLifeXis]: ahh, yeah, didn't look through nodes yet today. [erix]: haha [MidLifeXis]: Ahh well, time to get back to it, so there isn't a reason to take that choice out of my hands :-)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-03-23 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (286 votes). Check out past polls.