Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
39
Task/Topswops/Ada/topswops.adb
Normal file
39
Task/Topswops/Ada/topswops.adb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
with Ada.Integer_Text_IO, Generic_Perm;
|
||||
|
||||
procedure Topswaps is
|
||||
|
||||
function Topswaps(Size: Positive) return Natural is
|
||||
package Perms is new Generic_Perm(Size);
|
||||
P: Perms.Permutation;
|
||||
Done: Boolean;
|
||||
Max: Natural;
|
||||
|
||||
function Swapper_Calls(P: Perms.Permutation) return Natural is
|
||||
Q: Perms.Permutation := P;
|
||||
I: Perms.Element := P(1);
|
||||
begin
|
||||
if I = 1 then
|
||||
return 0;
|
||||
else
|
||||
for Idx in 1 .. I loop
|
||||
Q(Idx) := P(I-Idx+1);
|
||||
end loop;
|
||||
return 1 + Swapper_Calls(Q);
|
||||
end if;
|
||||
end Swapper_Calls;
|
||||
|
||||
begin
|
||||
Perms.Set_To_First(P, Done);
|
||||
Max:= Swapper_Calls(P);
|
||||
while not Done loop
|
||||
Perms.Go_To_Next(P, Done);
|
||||
Max := natural'Max(Max, Swapper_Calls(P));
|
||||
end loop;
|
||||
return Max;
|
||||
end Topswaps;
|
||||
|
||||
begin
|
||||
for I in 1 .. 10 loop
|
||||
Ada.Integer_Text_IO.Put(Item => Topswaps(I), Width => 3);
|
||||
end loop;
|
||||
end Topswaps;
|
||||
30
Task/Topswops/EasyLang/topswops.easy
Normal file
30
Task/Topswops/EasyLang/topswops.easy
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
global n d best[] .
|
||||
#
|
||||
proc tryswaps &a[] f .
|
||||
if d > best[n] : best[n] = d
|
||||
for s = len a[] downto 1
|
||||
if a[s] = 0 or a[s] = s : break 1
|
||||
if d + best[s] <= best[n] : return
|
||||
.
|
||||
d += 1
|
||||
for i = 1 to s : b[] &= a[i]
|
||||
k = 1
|
||||
for i = 2 to s
|
||||
k *= 2
|
||||
if b[i] = 0 and bitand f k = 0 or b[i] = i
|
||||
b[1] = i
|
||||
for j = i - 1 downto 1
|
||||
b[i - j + 1] = a[j]
|
||||
.
|
||||
tryswaps b[] (bitor f k)
|
||||
.
|
||||
.
|
||||
d -= 1
|
||||
.
|
||||
for n = 1 to 10
|
||||
best[] &= 0
|
||||
x[] &= 0
|
||||
d = 0
|
||||
tryswaps x[] 1
|
||||
print n & ": " & best[n]
|
||||
.
|
||||
56
Task/Topswops/JavaScript/topswops.js
Normal file
56
Task/Topswops/JavaScript/topswops.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
class Topswops {
|
||||
static maxBest = 32;
|
||||
static best = new Array(Topswops.maxBest).fill(0);
|
||||
|
||||
static trySwaps(deck, f, d, n) {
|
||||
if (d > this.best[n]) {
|
||||
this.best[n] = d;
|
||||
}
|
||||
for (let i = n - 1; i >= 0; i--) {
|
||||
if (deck[i] === -1 || deck[i] === i) {
|
||||
break;
|
||||
}
|
||||
if (d + this.best[i] <= this.best[n]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let deck2 = [...deck];
|
||||
for (let i = 1; i < n; i++) {
|
||||
const k = 1 << i;
|
||||
if (deck2[i] === -1) {
|
||||
if ((f & k) !== 0) {
|
||||
continue;
|
||||
}
|
||||
} else if (deck2[i] !== i) {
|
||||
continue;
|
||||
}
|
||||
deck2[0] = i;
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
deck2[i - j] = deck[j]; // Reverse copy.
|
||||
}
|
||||
this.trySwaps(deck2, f | k, d + 1, n);
|
||||
}
|
||||
}
|
||||
|
||||
static topswops(n) {
|
||||
if (n <= 0 || n >= this.maxBest) {
|
||||
throw new Error("n must be between 1 and maxBest");
|
||||
}
|
||||
this.best[n] = 0;
|
||||
let deck0 = new Array(n + 1).fill(0);
|
||||
for (let i = 1; i < n; i++) {
|
||||
deck0[i] = -1;
|
||||
}
|
||||
this.trySwaps(deck0, 1, 0, n);
|
||||
return this.best[n];
|
||||
}
|
||||
|
||||
static main() {
|
||||
for (let i = 1; i < 11; i++) {
|
||||
console.log(`${i}: ${this.topswops(i)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the main function
|
||||
Topswops.main();
|
||||
Loading…
Add table
Add a link
Reference in a new issue