Given an integer n ≥ 4, output a permutation of the integers [0, n-1] with the property that no two consecutive integers are next to each other. The value of a permutation pi is the sum of abs(pi[i] - i) for all indices i.

Examples

(1, 3, 0, 2) has value 6

(0, 2, 4, 1, 3) has value 6

(0, 2, 4, 1, 3, 5) has value 6

(0, 2, 4, 1, 5, 3, 6) has value 8

Score of your answer

The score of your answer is the sum of the values of your permutations for n = 4 .. 14 plus the number of bytes your code takes. The lower the score, the better. Your code must give valid output for all those values of n.

You must be able to run your submission to completion on your machine.

In case of ties, time of last edit that resulted in the relevant score will be the decider.

Answers to the linked question will not be competitive for this question as they make no effort to optimize the value of a permutation. For example for n=10, the permutation [1, 3, 5, 7, 9, 0, 2, 4, 6, 8] given by most of the answers there gives a value of 30. You can do much better than that.

For the permutation part of the question, the optimal value overall is at most 120. (Thank you to @Laikoni.) Whereas Dennis's answer to the previous question scores 222. (Thank you to @user202729.)

\$\begingroup\$@JoKing every answer can be ported without any changes, but would score terribly bad in this challenge. Posting that code in this challenge is equivalent to posting code from code-review to a code-golf challenge.\$\endgroup\$
– Stewie GriffinMay 15 '18 at 8:25

2

\$\begingroup\$Mixing different quantities in the score can indeed be problematic. The answer with the best algorithm can usually be ported over to any language, in which case the scoring reduces to normal code golf.\$\endgroup\$
– AngsMay 15 '18 at 8:57

4

\$\begingroup\$The optimal values are [6,6,6,8,10,12,12,12,14,16,18] for a score of 120. Interestingly this pattern can be found in A078706.\$\endgroup\$
– LaikoniMay 15 '18 at 9:46

3

\$\begingroup\$Ok, it starts to differ from A078706 with n=17, which can have a score of 20.\$\endgroup\$
– LaikoniMay 15 '18 at 10:01

4

\$\begingroup\$I can understand the challenge clearly and unambiguously. If you disagree and vote to close, leave a comment here.\$\endgroup\$
– user202729May 15 '18 at 13:53

This C++ program generates the lexicographically smallest permutation, assuming that |i - pi| ≤ width (where width is a hardcoded constant) for all 0 ≤ i < n, with time complexity about O(width2 × 22×width × n) (which is just O(n) for fixed width): Try it online!

How?

Write a C++ program attempting to solve the problem optimally.

Observe the pattern. We note that the sequence of all elements except 4 last ones is a prefix of

\$\begingroup\$Ah, you went with a different prefix to me. Mine starts 0 2 4 1 3 5 8 6, and has a bigger branching factor but doesn't have such a simple pattern.\$\endgroup\$
– Peter TaylorMay 15 '18 at 14:13

CJam (60 bytes + 120 = 180 score)

Dissection

{
_5/4* e# Work out how much of the hard-coded prefix to use
8e!961=7_)er e# Prefix [0 2 4 1 3 5 8 6]
e# I identified this by brute forcing up to n=10 and looking for patterns
e# I then used the identified prefix [0 2 4 1] to brute-force further
<:A e# Take the desired prefix of the hard-coded array, and store a copy in A
\,^e! e# Generate all permutations of the values in [0 .. n-1] which aren't in A
{A\+}% e# Prepend A to each of them
{ e# Filter...
2ew::- e# Take each difference of two consecutive elements
:z e# Find their absolute values
1& e# Test whether 1 is among those absolute values
! e# Reject if it is
},
{ e# Sort by...
_$.- e# Take pairwise differences of permutation with the identity
:z e# Absolute values
1b e# Add them (by interpreting in base 1)
}$
0= e# Take the first
}

Previous algorithm (132+116):

Bruteforces the correct number of jumps of length ±2 or ±3. Selects the last one that has the correct numbers in it, seems to work just fine and is a lot cheaper than implementing the score. Tio just runs out time before the last score, which is 18.

Japt, 120 + 20 = 140

(Copying one of my solutions from the other challenge would have scored me 227)

o á k_äa d¥1ÃñxÈaYÃg

Try it or use this version to check scores. Both versions may start crapping out on you around 9.

Explanation

o :Range [0,input)
á :All permutations
k_ Ã :Remove sub-arrays that return true
äa : Get the consecutive absolute differnces
d¥1 : Do any equal 1?
È Ã :Pass the integers in each remaining sub-array through a function
aY : Get the absolute difference with the integer's index
x :Reduce by addition
ñ :Sort the main array by those values
ñ :Return the first sub-array

\$\begingroup\$"You must be able to run your submission to completion on your machine." Did you seriously manage to process 87E9 permutations of 14 elements in the two hours since the question was posted?\$\endgroup\$
– Peter TaylorMay 15 '18 at 10:16

3

\$\begingroup\$Besides, consider that Japt is based on Javascript, can it really handle 87E9 permutations? This question say that Javascript array can have length of at most ~4E9. Does Japt have generating function or something...\\$\endgroup\$
– user202729May 15 '18 at 13:56

Try it online! Explanation: l keeps track of the last number generated and m keeps track of the next number of the opposite parity to l; once l exceeds m+2 the variables are exchanged. An adjustment is made at the start of the sequence so that sequences whose lengths are not multiples of 5 do not miss out any numbers, and another adjustment is made for n=4.

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).