How to resolve the algorithm Flipping bits game step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Flipping bits game step by step in the Raku programming language

Table of Contents

Problem Statement

Given an   N×N   square array of zeroes or ones in an initial configuration,   and a target configuration of zeroes and ones.

The game is to transform one to the other in as few moves as possible by inverting whole numbered rows or whole lettered columns at once   (as one move). In an inversion.   any  1  becomes  0,   and any  0  becomes  1  for that whole row or column.

Create a program to score for the Flipping bits game.

Show an example of a short game here, on this page, for a   3×3   array of bits.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Flipping bits game step by step in the Raku programming language

Source code in the raku programming language

sub MAIN ($square = 4) {
    say "{$square}? Seriously?" and exit if $square < 1 or $square > 26;
    my %bits = map { $_ => %( map { $_ => 0 }, ('A' .. *)[^ $square] ) },
        (1 .. *)[^ $square];
    scramble %bits;
    my $target = build %bits;
    scramble %bits until build(%bits) ne $target;
    display($target, %bits);
    my $turns = 0;
    while my $flip = prompt "Turn {++$turns}: Flip which row / column? " {
        flip $flip.match(/\w/).uc, %bits;
        if display($target, %bits) {
            say "Hurray! You solved it in $turns turns.";
            last;
        }
    }
}

sub display($goal, %hash) {
    shell('clear');
    say "Goal\n$goal\nYou";
    my $this = build %hash;
    say $this;
    return ($goal eq $this);
}

sub flip ($a, %hash) {
    given $a {
        when any(keys %hash) {
            %hash{$a}{$_} = %hash{$a}{$_} +^ 1 for %hash{$a}.keys
        };
        when any(keys %hash{'1'}) {
            %hash{$_}{$a} = %hash{$_}{$a} +^ 1 for %hash.keys
        };
    }
}

sub build (%hash) {
    my $string = '   ';
    $string ~= sprintf "%2s ", $_ for sort keys %hash{'1'};
    $string ~= "\n";
    for %hash.keys.sort: +* -> $key {
        $string ~= sprintf "%2s ", $key;
        $string ~= sprintf "%2s ", %hash{$key}{$_} for sort keys %hash{$key};
        $string ~=  "\n";
    };
    $string
}

sub scramble(%hash) {
    my @keys = keys %hash;
    @keys.push: | keys %hash{'1'};
    flip $_,  %hash for @keys.pick( @keys/2 );
}


  

You may also check:How to resolve the algorithm Y combinator step by step in the Chapel programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the C# programming language
You may also check:How to resolve the algorithm A+B step by step in the Lua programming language
You may also check:How to resolve the algorithm Empty program step by step in the Simula programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Modula-3 programming language