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,24 @@
int[][] zigZag(in int n) pure nothrow @safe {
static void move(in int n, ref int i, ref int j)
pure nothrow @safe @nogc {
if (j < n - 1) {
if (i > 0) i--;
j++;
} else
i++;
}
auto a = new int[][](n, n);
int x, y;
foreach (v; 0 .. n ^^ 2) {
a[y][x] = v;
(x + y) % 2 ? move(n, x, y) : move(n, y, x);
}
return a;
}
void main() {
import std.stdio;
writefln("%(%(%2d %)\n%)", 5.zigZag);
}

View file

@ -0,0 +1,18 @@
import std.stdio, std.algorithm, std.range, std.array;
int[][] zigZag(in int n) pure nothrow {
static struct P2 { int x, y; }
const L = iota(n ^^ 2).map!(i => P2(i % n, i / n)).array
.sort!q{ (a.x + a.y == b.x + b.y) ?
((a.x + a.y) % 2 ? a.y < b.y : a.x < b.x) :
(a.x + a.y) < (b.x + b.y) }.release;
auto result = new typeof(return)(n, n);
foreach (immutable i, immutable p; L)
result[p.y][p.x] = i;
return result;
}
void main() {
writefln("%(%(%2d %)\n%)", 5.zigZag);
}