RosettaCodeData/Task/Langtons-ant/D/langtons-ant-1.d
2023-07-01 13:44:08 -04:00

26 lines
847 B
D

void main() @safe {
import std.stdio, std.traits;
enum width = 75, height = 52;
enum maxSteps = 12_000;
enum Direction { up, right, down, left }
enum Color : char { white = '.', black = '#' }
uint x = width / 2, y = height / 2;
Color[width][height] M;
auto dir = Direction.up;
with (Color)
for (int i = 0; i < maxSteps && x < width && y < height; i++) {
immutable turn = M[y][x] == black;
dir = [EnumMembers!Direction][(dir + (turn ? 1 : -1)) & 3];
M[y][x] = (M[y][x] == black) ? white : black;
final switch(dir) with (Direction) {
case up: y--; break;
case right: x--; break;
case down: y++; break;
case left: x++; break;
}
}
writefln("%(%-(%c%)\n%)", M);
}