Topswops: Difference between revisions

1,155 bytes added ,  2 years ago
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible, minor tidy)
Line 1,672:
</pre>
It will manage 10 under pwa/p2js but with a blank screen for 38s, so I've capped it to 9 to make it finish in 3s.
 
=={{header|Picat}}==
<lang Picat>go ?=>
member(N,1..10),
Perm = 1..N,
Rev = Perm.reverse(),
Max = 0,
while(Perm != Rev)
next_permutation(Perm),
C = topswops(Perm),
if C > Max then
Max := C
end
end,
printf("%2d: %2d\n",N,Max),
fail,
nl.
go => true.
 
topswops([]) = 0 => true.
topswops([1]) = 0 => true.
topswops([1|_]) = 0 => true.
topswops(P) = Count =>
Len = P.length,
Count = 0,
while (P[1] > 1)
Pos = P[1],
P := [P[I] : I in 1..Pos].reverse() ++ [P[I] : I in Pos+1..Len],
Count := Count + 1
end.
 
% Inline
next_permutation(Perm) =>
N = Perm.length,
K = N - 1,
while (Perm[K] > Perm[K+1], K >= 0)
K := K - 1
end,
if K > 0 then
J = N,
while (Perm[K] > Perm[J]) J := J - 1 end,
Tmp := Perm[K],
Perm[K] := Perm[J],
Perm[J] := Tmp,
R = N,
S = K + 1,
while (R > S)
Tmp := Perm[R],
Perm[R] := Perm[S],
Perm[S] := Tmp,
R := R - 1,
S := S + 1
end
end.</lang>
 
{{out}}
<pre> 1: 0
2: 1
3: 2
4: 4
5: 7
6: 10
7: 16
8: 22
9: 30
10: 38</pre>
 
 
=={{header|PicoLisp}}==
495

edits