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,9 +1,9 @@
import std.stdio, std.algorithm, std.range, permutations2;
int topswops(in int n) {
static int flip(int[] xa) pure nothrow {
int topswops(in int n) pure @safe {
static int flip(int[] xa) pure nothrow @safe @nogc {
if (!xa[0]) return 0;
xa[0 .. xa[0] + 1].reverse(); // Slow with DMD.
xa[0 .. xa[0] + 1].reverse();
return 1 + flip(xa);
}
return n.iota.array.permutations.map!flip.reduce!max;

View file

@ -1,26 +1,19 @@
import std.stdio, std.typetuple;
template Range(int start, int stop) {
static if (stop <= start)
alias TypeTuple!() Range;
else
alias TypeTuple!(Range!(start, stop - 1), stop - 1) Range;
}
import std.stdio, std.typecons;
__gshared uint[32] best;
uint topswops(size_t n)() nothrow {
uint topswops(size_t n)() nothrow @nogc {
static assert(n > 0 && n < best.length);
size_t d = 0;
alias T = byte;
alias Deck = T[n];
void trySwaps(in ref Deck deck, in uint f) nothrow {
void trySwaps(in ref Deck deck, in uint f) nothrow @nogc {
if (d > best[n])
best[n] = d;
foreach_reverse (immutable i; Range!(0, n)) {
foreach_reverse (immutable i; staticIota!(0, n)) {
if ((deck[i] == i || (deck[i] == -1 && !(f & (1U << i))))
&& (d + best[i] >= best[n] || deck[i] == -1))
break;
@ -29,17 +22,17 @@ uint topswops(size_t n)() nothrow {
}
Deck deck2 = void;
foreach (immutable i; Range!(0, n)) // Copy.
foreach (immutable i; staticIota!(0, n)) // Copy.
deck2[i] = deck[i];
d++;
foreach (immutable i; Range!(1, n)) {
foreach (immutable i; staticIota!(1, n)) {
enum uint k = 1U << i;
if (deck[i] != i && (deck[i] != -1 || (f & k)))
continue;
deck2[0] = cast(T)i;
foreach_reverse (immutable j; Range!(0, i))
deck2[0] = T(i);
foreach_reverse (immutable j; staticIota!(0, i))
deck2[i - j] = deck[j]; // Reverse copy.
trySwaps(deck2, f | k);
}
@ -54,6 +47,6 @@ uint topswops(size_t n)() nothrow {
}
void main() {
foreach (i; Range!(1, 14))
foreach (immutable i; staticIota!(1, 14))
writefln("%2d: %d", i, topswops!i());
}

View file

@ -0,0 +1,109 @@
class
TOPSWOPS
create
make
feature
make(n: INTEGER)
local
perm,ar: ARRAY[INTEGER]
i,j,k, tcount, count: INTEGER
do
create perm_sol.make_empty
create solution.make_empty
from j:= 1
until j> n
loop
create ar.make_filled (0, 1, j)
from
k:=1
until
k>j
loop
ar[k]:=k
k:= k+1
end
permute(ar, 1)
from
i:= 1
until
i> perm_sol.count
loop
tcount:= 0
from
until
perm_sol.at (i).at (1)=1
loop
perm_sol.at(i):=reverse_array(perm_sol.at(i))
tcount:= tcount+1
end
if tcount>count then
count:= tcount
end
i:= i+1
end
solution.force(count, j)
j:=j+1
end
end
solution: ARRAY[INTEGER]
feature {NONE}
perm_sol: ARRAY[ARRAY[INTEGER]]
reverse_array(ar:ARRAY[INTEGER]):ARRAY[INTEGER]
require
ar_not_void: ar /= void
local
i,j:INTEGER
new_array: ARRAY[INTEGER]
do
create new_array.make_empty
new_array.copy(ar)
from
i:= 1
j:=ar[1]
until
i>j
loop
new_array[i]:=ar[j]
new_array[j]:=ar[i]
i:=i+1
j:=j-1
end
Result:= new_array
ensure
same_length: ar.count = Result.count
end
permute(a: ARRAY[INTEGER]; k: INTEGER)
require
ar_not_void: a.count>=1
k_valid_index: k>0
local
i,t: INTEGER
temp: ARRAY[INTEGER]
do
create temp.make_empty
if k=a.count then
across a as ar loop temp.force (ar.item, temp.count+1) end
perm_sol.force(temp, perm_sol.count+1)
else
from
i:= k
until
i> a.count
loop
t:= a[k]
a[k]:= a[i]
a[i]:= t
permute(a,k+1)
t:= a[k]
a[k]:= a[i]
a[i]:= t
i:= i+1
end
end
end
end

View file

@ -0,0 +1,14 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create ts.make (10)
across ts.solution as t loop io.put_string (t.item.out+"%N") end
end
ts: TOPSWOPS
end

View file

@ -1,8 +1,6 @@
def f1(a)
i = 0
loop do
a0 = a[0]
break if a0 == 1
while (a0 = a[0]) > 1
a[0...a0] = a[0...a0].reverse
i += 1
end

View file

@ -0,0 +1,32 @@
object Fannkuch extends App {
def fannkuchen(l: List[Int], n: Int, i: Int, acc: Int): Int = {
def flips(l: List[Int]): Int = (l: @unchecked) match {
case 1 :: ls => 0
case (n :: ls) =>
val splitted = l.splitAt(n)
flips(splitted._2.reverse_:::(splitted._1)) + 1
}
def rotateLeft(l: List[Int]) =
l match {
case Nil => List()
case x :: xs => xs ::: List(x)
}
if (i >= n) acc
else {
if (n == 1) acc.max(flips(l))
else {
val split = l.splitAt(n)
fannkuchen(rotateLeft(split._1) ::: split._2, n, i + 1, fannkuchen(l, n - 1, 0, acc))
}
}
} // def fannkuchen(
val result = (1 to 10).map(i => (i, fannkuchen(List.range(1, i + 1), i, 0, 0)))
println("Computing results...")
result.foreach(x => println(s"Pfannkuchen(${x._1})\t= ${x._2}"))
assert(result == Vector((1, 0), (2, 1), (3, 2), (4, 4), (5, 7), (6, 10), (7, 16), (8, 22), (9, 30), (10, 38)), "Bad results")
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}