Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re^2: Lights out puzzle (non-non-perl solution) by salva
in thread Lights out puzzle by ambrus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (3)
    As of 2014-09-02 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (19 votes), past polls