How to resolve the algorithm Resistor mesh step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Resistor mesh step by step in the Raku programming language

Table of Contents

Problem Statement

Given   10×10   grid nodes   (as shown in the image)   interconnected by   1Ω   resistors as shown, find the resistance between points   A   and   B.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Resistor mesh step by step in the Raku programming language

Source code in the raku programming language

my $*TOLERANCE = 1e-12;

sub set-boundary(@mesh,@p1,@p2) {
    @mesh[ @p1[0] ; @p1[1] ] =  1;
    @mesh[ @p2[0] ; @p2[1] ] = -1;
}

sub solve(@p1, @p2, Int \w, Int \h) {
    my @d     = [0 xx w] xx h;
    my @V     = [0 xx w] xx h;
    my @fixed = [0 xx w] xx h;
    set-boundary(@fixed,@p1,@p2);

    loop {
        set-boundary(@V,@p1,@p2);
        my $diff = 0;
        for (flat ^h X ^w) -> \i, \j {
            my @neighbors = (@V[i-1;j], @V[i;j-1], @V[i+1;j], @V[i;j+1]).grep: *.defined;
            @d[i;j] = my \v = @V[i;j] - @neighbors.sum / @neighbors;
            $diff += v × v unless @fixed[i;j];
        }
        last if $diff =~= 0;

        for (flat ^h X ^w) -> \i, \j {
            @V[i;j] -= @d[i;j];
        }
    }

    my @current;
    for (flat ^h X ^w) -> \i, \j {
        @current[ @fixed[i;j]+1 ] += @d[i;j] × (?i + ?j + (i < h-1) + (j < w-1) );
    }
    (@current[2] - @current[0]) / 2
}

say 2 / solve (1,1), (6,7), 10, 10;


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the PIR programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the 11l programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Plan programming language
You may also check:How to resolve the algorithm System time step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Picat programming language