RosettaCodeData/Task/Topswops/Jq/topswops-2.jq
2023-07-01 13:44:08 -04: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);