Skip to content

Instantly share code, notes, and snippets.

@codekitchen
Created October 31, 2013 18:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codekitchen/7254133 to your computer and use it in GitHub Desktop.
Save codekitchen/7254133 to your computer and use it in GitHub Desktop.
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