Created
October 31, 2013 18:01
-
-
Save codekitchen/7254133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.string; | |
import std.conv; | |
import std.array; | |
import std.range; | |
void times(int count, void delegate() cb) { | |
for (int i = 0; i < count; ++i) { | |
cb(); | |
} | |
} | |
alias char[][] Field; | |
char get(Field field, size_t x, size_t y) { | |
if (x < 0 || y < 0 || x >= field[0].length || y >= field.length) | |
return 0; | |
if (field[y][x] == '*') | |
return 1; | |
return 0; | |
} | |
void main() { | |
foreach(field_num; 1 .. int.max) { | |
stdout.writefln("Field #%d:", field_num); | |
auto dims = stdin.readln().strip.split(); | |
int rows = dims[0].to!int; | |
int cols = dims[1].to!int; | |
if (rows == 0) | |
return; | |
Field field; | |
rows.times({ field ~= stdin.readln().strip.dup; }); | |
foreach (y, row; field) { | |
foreach (x, ref cell; row) { | |
if (cell == '*') | |
continue; | |
cell = cast(char)('0' + | |
get(field, x-1, y-1) + | |
get(field, x-1, y) + | |
get(field, x-1, y+1) + | |
get(field, x, y-1) + | |
get(field, x, y+1) + | |
get(field, x+1, y-1) + | |
get(field, x+1, y) + | |
get(field, x+1, y+1)); | |
} | |
} | |
stdout.writeln(field.join("\n")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment