Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

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

View file

@ -0,0 +1,14 @@
import std.string, std.range, std.algorithm;
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 d.join('\n');
}
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;
}
}