#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Wireworld
#

var f = [[], DATA.lines.map {['', .chomp.chars..., '']}..., []];

var gens = [];

range(1, 10).each {
    gens << f;
    var a = [[]];
    range(1, f.end-1).each { |y|
        var r = f[y];
        var rr = [''];
        range(1, r.end-1).each { |x|
            var c = r[x];
            rr.append(
              given(c) {
                when('H') { 't' }
                when('t') { '.' }
                when('.') { <. H>[f.slice(y-1, 3).map{.slice(x-1, 3)...}.count('H') ~~ [1,2]] }
                default   { c }
              }
            )
        }
        rr.append('');
        a.append(rr);
    }
    f = [a..., []];
}

var gen10 = <<'EOT'.lines.map{.strip}.join("\n");

 t H . t H . t H . t H
 .       H
       . . .
 .       .
 H t H .   . . . . . .
EOT

var t = gens[-1].map { .join(" ") }.map{.strip}.join("\n").chomp;
assert_eq(t, gen10);

say "** Test passed!";

__DATA__
tH.........
.   .
   ...
.   .
Ht.. ......
