g | x | w | all
Bytes Lang Time Link
019Uiua241205T133525Znyxbird
008Vyxal r230219T171752ZThe Thon
714Nibbles230219T231319ZDominic
010Stax180324T174701Zrecursiv
014Jelly180324T141251ZErik the
010Brachylog v2180323T221038Zais523
080J120630T061233ZGareth
038GolfScript120628T172023ZHoward
032GolfScript120629T212753ZPeter Ta

Uiua, 19 bytes

⍣⊢∘▽/↧⧈≠⍉.⧅≠⊸⧻

Try it!

           ⧅≠⊸⧻ # take all permutations of the input
    /↧⧈≠⍉       # make a mask of those for which no adjacent elements are equal
⍣⊢∘▽      .      # and get the first (defaulting to an empty array)

Vyxal r, 12 8 bytes

Ṗ‡ÞǓ⁼c¾∨

Try it Online!

-4 thanks to @Steffan

Explanation (old)

I'll update it when I have time.

Ṗ¾wJλ:Ḣ=a¬;c  # Implicit input
Ṗ             # Permutations
 ¾wJ          # With an empty list appended
              # (In case there is no truthy item)
    λ     ;c  # First item such that:
     :Ḣ=      #  Adjacent elements are equal?
        a¬    #  None are true
              # Implicit output

Nibbles, 7 bytes (14 nibbles)

/|``p$/!>>$@!*

Systematically (non-randomly) finds all permutations of the input without adjacent identical nuts, and then returns the first of these (or an empty list if there aren't any).

/|``p$/!>>$@!*
 |               # filter
  ``p            # all permutations of 
     $           # the input list
                 # by 
      /      *   # fold-by-multiplication
       !         #   zip together
        >>$      #     list without first element
           @     #     the same list
            !    #     are elements equal?
/                # finally get first element

enter image description here

Stax, 10 bytes

│éÿ∞å[zàL⌂

Run and debug it

Here's the same program unpacked, ungolfed, and commented.

|T      get all permutations
{       block to filter by
  :g_=  after dropping repeated elements, it's still equal
f       execute filter
|c      terminate and pop if falsy (no match)
hJ      take the first permutation, and join with spaces

Run this one

Jelly, 14 bytes

Ġz0UẎḟ0ịµẋ⁻ƝẠ$

Try it online!

The last 6 bytes can be removed if we can have undefined behavior for invalid inputs.

Brachylog v2, 10 bytes

p.¬{s₂=}∨Ė

Try it online!

Brute-force solution. (This is a function, allowed because the challenge does not say "full program".) It's also mostly a direct translation of the spec (the only real subtlety is that I managed to arrange things so that all the implicit constraints arrived in exactly the right places, thus not needing any extra characters to disambiguate them).

Note that this is a generic algorithm for rearranging any sort of list so that it does not have two touching elements; it can handle string representations of the elements, and it can handle integer codes just as well. So it doesn't really matter how the "Your program must have a way of representing each kind of nut, such as an integer code." requirement from the question is interpreted.

Explanation

p.¬{s₂=}∨Ė
p            Find a permutation of {the input}
  ¬{   }     which does not have the following property:
    s₂         it contains a pair of adjacent elements
      =        that are equal
        ∨    {no constraint on what value the equal elements can have}
 .           If you find such a permutation, output it.
        ∨    If no permutation is found, ignore the input and
         Ė     {output} an empty list

J, 80 characters

]`_:@.(0<2&([:+/=/\))({.~-:@#),((],.|.)~>.@-:@#)<"1;(\:#&.>)(</.])[;.1' ',1!:1[1

Not really in the same league as Golfscript on this one. I suspect there are gains to be made, but the 14 characters needed just to get the list into the program [;.1' ',1!:1[1 is a major handicap.

Basically the program takes in the list, groups similar items together, sorts by number of items in each group descending, and alternates the output between the first half and the second half of the list. The rest if the code gets rid of extraneous items and decides if the list is valid output (outputting infinity _ if it isn't).

Example:

macadamia walnut walnut pistachio walnut

group (</.]):

macadamia walnut walnut walnut pistachio

sort (\:#&.>):

walnut walnut walnut macadamia pistachio

ravel ((],.|.)~>.@-:@#):

walnut macadamia walnut pistachio walnut

GolfScript, 42 41 37 38 characters

~.`{\`{=}+%1-,}+$.,)2//zip[]*.2<..&=*p

The code expects input on STDIN and prints result to STDOUT, e.g.:

> ["walnut" "walnut" "walnut" "macadamia" "pistachio"]
["walnut" "macadamia" "walnut" "pistachio" "walnut"]

> ["walnut" "walnut" "walnut" "macadamia" "walnut"]
[]

The script became longer than expected but I suppose there is room for improvement.

Edit: The case of a list with a single item costs me 1 character (the best comparison I could come up with is the same as Peter's).

GolfScript, 32 chars

~:x{]x\-,}$.,)2//zip[]*.2<..&=*`

Same input and output format as Howard's solution.