RosettaCodeData/Task/Wireworld/D/wireworld.d

39 lines
1.2 KiB
D
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
import std.stdio, std.algorithm;
2015-02-20 00:35:01 -05:00
void wireworldStep(char[][] W1, char[][] W2) pure nothrow @safe @nogc {
foreach (immutable r; 1 .. W1.length - 1)
foreach (immutable c; 1 .. W1[0].length - 1)
2013-04-11 01:07:29 -07:00
switch (W1[r][c]) {
case 'H': W2[r][c] = 't'; break;
case 't': W2[r][c] = '.'; break;
case '.':
int nH = 0;
foreach (sr; -1 .. 2)
foreach (sc; -1 .. 2)
nH += W1[r + sr][c + sc] == 'H';
W2[r][c] = (nH == 1 || nH == 2) ? 'H' : '.';
break;
default:
}
}
void main() {
auto world = [" ".dup,
" tH ".dup,
" . .... ".dup,
" .. ".dup,
" ".dup];
char[][] world2;
foreach (row; world)
world2 ~= row.dup;
2015-02-20 00:35:01 -05:00
foreach (immutable step; 0 .. 7) {
2013-04-11 01:07:29 -07:00
writefln("\nStep %d: ------------", step);
2015-02-20 00:35:01 -05:00
foreach (row; world[1 .. $ - 1])
row[1 .. $ - 1].writeln;
2013-04-11 01:07:29 -07:00
wireworldStep(world, world2);
swap(world, world2);
}
}