Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,6 +1,6 @@
import std.stdio, std.algorithm, std.string, std.array;
void main() /*@safe*/ {
import std.stdio, std.algorithm, std.string, std.array;
void main() {
enum level = 4;
auto d = ["*"];
foreach (immutable n; 0 .. level) {
@ -8,5 +8,5 @@ void main() {
d = d.map!(a => sp ~ a ~ sp).array ~
d.map!(a => a ~ " " ~ a).array;
}
d.join("\n").writeln;
d.join('\n').writeln;
}

View file

@ -1,19 +1,14 @@
import std.string;
import std.string, std.range, std.algorithm;
string sierpinski(int n) {
auto parts = ["*"];
auto space = " ";
foreach (i; 0 .. n) {
string[] parts2;
foreach (x; parts)
parts2 ~= space ~ x ~ space;
foreach (x; parts)
parts2 ~= x ~ " " ~ x;
parts = parts2;
space ~= space;
string sierpinski(int level) pure nothrow /*@safe*/ {
auto d = ["*"];
foreach (immutable i; 0 .. level) {
immutable sp = " ".replicate(2 ^^ i);
d = d.map!(a => sp ~ a ~ sp).array ~
d.map!(a => a ~ " " ~ a).array;
}
return parts.join("\n");
return d.join('\n');
}
pragma(msg, sierpinski(4));
pragma(msg, 4.sierpinski);
void main() {}

View file

@ -0,0 +1,17 @@
void showSierpinskiTriangle(in uint order) nothrow @safe @nogc {
import core.stdc.stdio: putchar;
foreach_reverse (immutable y; 0 .. 2 ^^ order) {
foreach (immutable _; 0 .. y)
' '.putchar;
foreach (immutable x; 0 .. 2 ^^ order - y) {
putchar((x & y) ? ' ' : '*');
' '.putchar;
}
'\n'.putchar;
}
}
void main() nothrow @safe @nogc {
4.showSierpinskiTriangle;
}

View file

@ -0,0 +1,42 @@
import core.stdc.stdio: putchar;
import std.algorithm: swap;
void showSierpinskiTriangle(in uint nLevels) nothrow @safe
in {
assert(nLevels > 0);
} body {
alias Row = bool[];
static void applyRules(in Row r1, Row r2) pure nothrow @safe @nogc {
r2[0] = r1[0] || r1[1];
r2[$ - 1] = r1[$ - 2] || r1[$ - 1];
foreach (immutable i; 1 .. r2.length - 1)
r2[i] = r1[i - 1] != r1[i] || r1[i] != r1[i + 1];
}
static void showRow(in Row r) nothrow @safe @nogc {
foreach (immutable b; r)
putchar(b ? '#' : ' ');
'\n'.putchar;
}
immutable width = 2 ^^ (nLevels + 1) - 1;
auto row1 = new Row(width);
auto row2 = new Row(width);
row1[width / 2] = true;
foreach (immutable _; 0 .. 2 ^^ nLevels) {
showRow(row1);
applyRules(row1, row2);
row1.swap(row2);
}
}
void main() @safe nothrow {
foreach (immutable i; 1 .. 6) {
i.showSierpinskiTriangle;
'\n'.putchar;
}
}