Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Ever see that puzzle with the 5x5 grid where you have to toggle the colors until you get the whole grid to be the same color? (it would start one color, and you had to get it to be the other color). Well, I was trying to remember the solution, so I coded up the puzzle. It can be solved in 15 moves, I will post the answer next week. Here is my code for the puzzle:
#!perl -w use strict; { package Board; my $white = '_'; my $black = '#'; # Construct a new board. sub new { my $proto = shift || die "Expected class"; my $class = ref($proto) || $proto; my $self = [[($white)x5],[($white)x5],[($white)x5], [($white)x5],[($white)x5]]; bless ($self, $class); return $self; } # Returns true if the board is all black. False otherwise. sub Finished { my $self = shift || die "Expected self"; for( @$self ) { for( @$_ ) { return 0 if $white eq $_; } } return 1; } # Returns a display version of the board, suitable for printing. sub Display { my $self = shift || die "Expected self"; my $rowN = 0; my @disp = map {"$_\n"} " 12345", map {++$rowN . join('', @$_) +} @$self; return wantarray ? @disp : join '', @disp; } # Toggle the location given, and the locations to the N, E, S, & W sub Toggle { die "Incorrect number of args" unless 3 == @_; my ( $self, $x, $y ) = @_; die "X ($x) out of range\n" if $x > 5 or 1 > $x; die "Y ($y) out of range\n" if $y > 5 or 1 > $y; $self->_ToggleSquare( $x, $y ); $self->_ToggleSquare( $x+1, $y ); $self->_ToggleSquare( $x-1, $y ); $self->_ToggleSquare( $x, $y+1 ); $self->_ToggleSquare( $x, $y-1 ); return undef; } # Toggle the given location. No-op if location is out of range. sub _ToggleSquare { die "Incorrect number of args" unless 3 == @_; my ( $self, $x, $y ) = @_; return undef if $x > 5 or 1 > $x or $y > 5 or 1 > $y; ($x, $y) = map { $_ - 1 } $x, $y; $self->[$x]->[$y] = $white eq $self->[$x]->[$y] ? $black : $wh +ite; return undef; } } sub GetMove { print "> "; my $move = <>; # Read from STDIN, or a file. my ($control, $x, $y) = $move =~ m/^\s*([a-zA-Z]+|(\d)[\/,\s-]?(\d +))\s*$/; ($x, $y) = GetMove() if not defined $control; # Repeat as necessar +y die "Game Over\n" if $control && $control =~ m/^[qQeE]/; # quit, e +xit, etc. return ($x, $y); } sub main { my $board = Board->new(); my @moves = (); while( not $board->Finished() ) { print $board->Display(); my @move = GetMove(); push @moves, \@move; $board->Toggle( @move ); } print "Success! ", scalar( @moves ), " moves.\n"; print join( "\n", map { join '-', @$_ } @moves ), "\n"; return 0; } exit( main() );

In reply to 5x5 Puzzle by Adam

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.
Log In?
Username:
Password:

What's my password?
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 romping around the Monastery: (7)
As of 2024-03-28 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found