This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -1,7 +1,7 @@
import std.string, std.typecons, std.exception, std.algorithm;
import queue_usage2; // No queue in DMD 2.061 Phobos.
import queue_usage2; // No queue in Phobos 2.063.
immutable struct Board {
const struct Board {
private enum El : char { floor=' ', wall='#', goal='.',
box='$', player='@', boxOnGoal='*' }
private alias string CTable;
@ -9,7 +9,7 @@ immutable struct Board {
private immutable CTable sData, dData;
private immutable int playerx, playery;
this(in string[] board) pure nothrow
this(in string[] board) pure nothrow const
in {
foreach (row; board) {
assert(row.length == board[0].length,
@ -36,34 +36,34 @@ immutable struct Board {
private bool move(in int x, in int y, in int dx,
in int dy, ref CTable data)
const pure /*nothrow*/ {
if (sData[(y+dy) * ncols + x+dx] == El.wall ||
data[(y+dy) * ncols + x+dx] != El.floor)
if (sData[(y+dy) * ncols + x + dx] == El.wall ||
data[(y+dy) * ncols + x + dx] != El.floor)
return false;
auto data2 = data.dup; // not nothrow
auto data2 = data.dup; // Not nothrow.
data2[y * ncols + x] = El.floor;
data2[(y+dy) * ncols + x+dx] = El.player;
data = assumeUnique(data2); // not enforced
data2[(y + dy) * ncols + x + dx] = El.player;
data = data2.assumeUnique; // Not enforced.
return true;
}
private bool push(in int x, in int y, in int dx,
in int dy, ref CTable data)
const pure /*nothrow*/ {
if (sData[(y+2*dy) * ncols + x+2*dx] == El.wall ||
data[(y+2*dy) * ncols + x+2*dx] != El.floor)
if (sData[(y + 2*dy) * ncols + x+2*dx] == El.wall ||
data[(y + 2*dy) * ncols + x+2*dx] != El.floor)
return false;
auto data2 = data.dup; // not nothrow
auto data2 = data.dup; // Not nothrow.
data2[y * ncols + x] = El.floor;
data2[(y+dy) * ncols + x+dx] = El.player;
data2[(y+2*dy) * ncols + x+2*dx] = El.boxOnGoal;
data = assumeUnique(data2); // not enforced
data2[(y + dy) * ncols + x + dx] = El.player;
data2[(y + 2*dy) * ncols + x + 2*dx] = El.boxOnGoal;
data = data2.assumeUnique; // Not enforced.
return true;
}
private bool isSolved(in CTable data) const pure nothrow {
foreach (i, d; data)
foreach (immutable i, immutable d; data)
if ((sData[i] == El.goal) != (d == El.boxOnGoal))
return false;
return true;
@ -82,8 +82,8 @@ immutable struct Board {
tuple(-1, 0, 'l', 'L')];
while (open.length) {
//immutable (cur, cSol, x, y) = open.pop();
immutable item = open.pop();
//immutable (cur, cSol, x, y) = open.pop;
immutable item = open.pop;
immutable CTable cur = item[0];
immutable string cSol = item[1];
immutable int x = item[2];
@ -95,7 +95,7 @@ immutable struct Board {
immutable int dx = di[0];
immutable int dy = di[1];
if (temp[(y+dy) * ncols + x+dx] == El.boxOnGoal) {
if (temp[(y + dy) * ncols + x + dx] == El.boxOnGoal) {
if (push(x, y, dx, dy, temp) && temp !in visitedSet) {
if (isSolved(temp))
return cSol ~ di[3];
@ -118,7 +118,8 @@ immutable struct Board {
void main() {
import std.stdio, core.memory;
GC.disable(); // Uses about twice the memory
GC.disable; // Uses about twice the memory
immutable level =
"#######
# #
@ -129,6 +130,6 @@ void main() {
#.# @#
#######";
immutable b = Board(level.splitLines());
writeln(level, "\n\n", b.solve());
const b = const(Board)(level.splitLines);
writeln(level, "\n\n", b.solve);
}