RosettaCodeData/Task/Topswops/Jq/topswops-2.jq
2017-09-25 22:28:19 +02:00

13 lines
319 B
Text

# Input: a permutation; output: an integer
def flips:
# state: [i, array]
[0, .]
| until( .[1][0] == 1;
.[1] as $p | $p[0] as $p0
| [.[0] + 1, ($p[:$p0] | reverse) + $p[$p0:] ] )
| .[0];
# input: n, the number of items
def fannkuch:
reduce permutations as $p
(0; [., ($p|flips) ] | max);