# Wiggle the tower

Your input is the height map of a tower. The tower is made out of layers, each one being one unit shorter than the one below it. Every layer is completely on top of the previous layer. For example [1,5,4,3,2] is a valid input. Here is a visualization:

 #
##
###
####
#####

15432


Your task is to wiggle the tower. Wiggling works as from top to bottom. For each layer, we first try to shift left. If we can, we shift left and then stop. If we can't, we shift the layer to the right and repeat for the next layer. If we reach the bottom layer we stop. The bottom layer doesn't move anywhere. When a layer is shifted, the layers above it also move the same amount in the same direction.

Here is a wiggle, step by step:

 #
##
###
####
#####

#
##
###
####
#####
Top layer couldn't move left, so move right and repeat

#
##
###
####
#####
Layer 2 couldn't move left, so move right and repeat

#
##
###
####
#####
Layer 3 couldn't move left, so move right and repeat

#
##
###
####
#####
Layer 4 could move left, so the wiggle stops here.
Wiggling would anyway finish at the second lowest layer, since the lowest layer is immovable.


This means that if your program got input [1,5,4,3,2] it should return [2,3,4,5,1].

Because of the mathematics of tower wiggling, your input is always a permutation of the positive integers up to the array length, and also there are $$\2^{n-1}\$$ valid inputs (and outputs) for arrays of length $$\n\$$.

# Test cases (all towers up to $$\n=10\$$)

[1] -> [1]
[1, 2] -> [2, 1] -> [1, 2]
[1, 2, 3] -> [1, 3, 2] -> [2, 3, 1] -> [3, 2, 1] -> [1, 2, 3]
[1, 2, 3, 4] -> [1, 2, 4, 3] -> [1, 3, 4, 2] -> [1, 4, 3, 2] -> [2, 3, 4, 1] -> [2, 4, 3, 1] -> [3, 4, 2, 1] -> [4, 3, 2, 1] -> [1, 2, 3, 4]
[1, 2, 3, 4, 5] -> [1, 2, 3, 5, 4] -> [1, 2, 4, 5, 3] -> [1, 2, 5, 4, 3] -> [1, 3, 4, 5, 2] -> [1, 3, 5, 4, 2] -> [1, 4, 5, 3, 2] -> [1, 5, 4, 3, 2] -> [2, 3, 4, 5, 1] -> [2, 3, 5, 4, 1] -> [2, 4, 5, 3, 1] -> [2, 5, 4, 3, 1] -> [3, 4, 5, 2, 1] -> [3, 5, 4, 2, 1] -> [4, 5, 3, 2, 1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-> [5, 7, 9, 10, 8, 6, 4, 3, 2, 1] -> [5, 7, 10, 9, 8, 6, 4, 3, 2, 1] -> [5, 8, 9, 10, 7, 6, 4, 3, 2, 1] -> [5, 8, 10, 9, 7, 6, 4, 3, 2, 1] -> [5, 9, 10, 8, 7, 6, 4, 3, 2, 1] -> [5, 10, 9, 8, 7, 6, 4, 3, 2, 1] -> [6, 7, 8, 9, 10, 5, 4, 3, 2, 1] -> [6, 7, 8, 10, 9, 5, 4, 3, 2, 1] -> [6, 7, 9, 10, 8, 5, 4, 3, 2, 1] -> [6, 7, 10, 9, 8, 5, 4, 3, 2, 1] -> [6, 8, 9, 10, 7, 5, 4, 3, 2, 1] -> [6, 8, 10, 9, 7, 5, 4, 3, 2, 1] -> [6, 9, 10, 8, 7, 5, 4, 3, 2, 1] -> [6, 10, 9, 8, 7, 5, 4, 3, 2, 1] -> [7, 8, 9, 10, 6, 5, 4, 3, 2, 1] -> [7, 8, 10, 9, 6, 5, 4, 3, 2, 1] -> [7, 9, 10, 8, 6, 5, 4, 3, 2, 1] -> [7, 10, 9, 8, 6, 5, 4, 3, 2, 1] -> [8, 9, 10, 7, 6, 5, 4, 3, 2, 1] -> [8, 10, 9, 7, 6, 5, 4, 3, 2, 1] -> [9, 10, 8, 7, 6, 5, 4, 3, 2, 1] -> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

• @Jonah the example is correct. Here is a step by step visualization of how the tower moves (2->3). Commented Feb 19, 2022 at 16:18
• @Jonah If by "step 5" you mean lines 28-32, then the answer is no. The wiggling process continues, because the layer above moved right. It only ends when layer 4 moves to the left. Perhaps the confusion comes from the fact that in 15432->23451, layers 1,2 and 4 change, while layer 3 doesn't. This is because when a layer moves, the layers on top will "go along" the ride. Here layer 3 first moves right and then layer 4 moves left, resulting in zero net movement. Commented Feb 19, 2022 at 16:31
• It's not at all clear to me from the description that the input will be a permutation of 1..n. I think you should just say that upfront. Commented Feb 19, 2022 at 17:56
• @AnttiP question about test case 2 3 1 -> 3 2 1. I get a different answer when manually going through the shifts: Try it online! Commented Feb 19, 2022 at 21:28
• @Jonah if we can move left at a layer, we stop. (It's a bit hidden away in the example ..."Layer 4 could move left, so the wiggle stops here. Wiggling would anyway finish at the second lowest layer...", so it could be made clearer. Commented Feb 19, 2022 at 23:17

# JavaScript (Node.js), 126 121 bytes

-5 byte from Neil

f=(x,n=x.length,s=n,j,k=(x+'').match((.*?)((\\d+,)?${s})\\b(.*)))=>k?f(x,--n,[s,n],k):j[1]+j[2].split,.reverse()+j[4]  Try it online! Reverse (_,)n,n-1,n-2,...,n-k which seems correct but long • Can you use .split, instead of .match(/\d+/g)? – Neil Commented Feb 20, 2022 at 9:56 # Python NumPy, 57 bytes def f(L):a=L.argmax();m=L>=(a and L[a-1]);L[m]=L[m][::-1] Attempt This Online! ### Old Python NumPy, 59 bytes def f(L):a=L.argmax();m=a and L[a-1];b=L>=m;L[b]=L[b][::-1] Attempt This Online! Port of @l4m2's JS. ### Stupid Python NumPy, 122 bytes def f(L,i=-2): a,*b=L.argsort()[i:];b.sort() if[a]<b:L[[a,*b]]=L[[*b,a]] else:L[[*b,a]]=L[[a,*b]];-i<len(L)and f(L,i-1) Attempt This Online! Takes a numpy array and modifies it in-place. Pretty straight-forward algo. # 05AB1E, 9 bytes Z¡нθO@ÏÂ‡  Port of the approach @l4m2 used in his JavaScript answer, so make sure to upvote him/her as well! Try it online or verify the test cases up to length 6 (because any more will be too big for TIO's STDOUT). Explanation:  # e.g. input=[1,3,5,10,9,8,7,6,4,2] Z # Get the maximum of the (implicit) input-list # STACK: [1,3,5,10,9,8,7,6,4,2],10 ¡ # Split the input-list by this maximum # STACK: [[1,3,5],[9,8,7,6,4,2]] н # Leave the first list of the pair (the items before the maximum) # STACK: [1,3,5] θ # Leave the last value of that list (the value just before the maximum) # STACK: 5 O # Sum in case the maximum was the very first item, resulting in 0 # STACK: 5 @ # Check for each value in the (implicit) input-list if it's >= this item # before the maximum # STACK: [0,0,1,1,1,1,1,1,0,0] Ï # Only leave the items of the (implicit) input-list at the truthy positions # STACK: [5,10,9,8,7,6] Â # Bifurcate this list (short for Duplicate & Reverse copy) # STACK: [5,10,9,8,7,6],[6,7,8,9,10,5] ‡ # Transliterate the values in the (implicit) input-list, basically reverting # the part that was truthy earlier # STACK: [1,3,6,7,8,9,10,5,4,2] # (after which it is output implicitly as result)  • I don't think you're actually using @l4m2's approach; yours is better, in that it saves me a byte in my Retina answer. – Neil Commented Feb 20, 2022 at 9:58 # R, 56 bytes Or R>=4.1, 49 bytes by replacing the word function with a \. function(v){v[i]=rev(v[i<-v>=sum(v[which.max(v)-1])]);v}  Try it online! Port of @l4m2's answer with the help of @Kevin Cruijssen's explanation. # 80386 machine code, 63 59 bytes \x60\x8d\x1c\x91\x89\xde\x83\xee\x04\x8b\x06\x89\xd1\x29\xc1\x39\xd0\x75\x05 \x48\x01\xd1\xeb\x03\x89\xd0\x41\x89\xf7\xe2\x0e\xa5\x40\x89\x46\xfc\x83\xef \x08\x39\xf7\x77\xf4\x61\xc3\x83\xc7\x04\x39\xdf\x74\xd2\x39\x07\x75\xce\x48 \xeb\xe2  Try it online! Thanks to @l4m2 suggesting pusha/popa saving 4 bytes! assembly (nasm) wiggle: ; fastcall, ecx = pointer to array, edx = length of array pusha lea ebx, [ecx + edx * 4] mov esi, ebx .L0: sub esi, 4 mov eax, [esi] mov ecx, edx sub ecx, eax cmp eax, edx jne .0 dec eax add ecx, edx jmp .1 .0: mov eax, edx inc ecx .1: mov edi, esi .L1: loop .2 .L2: movsd inc eax mov [esi - 4], eax sub edi, 8 cmp edi, esi ja .L2 popa ret .2: add edi, 4 cmp edi, ebx je .L0 cmp [edi], eax jne .L0 dec eax jmp .L1  • f you want to protect registers, why not pushad/popad? – l4m2 Commented Feb 20, 2022 at 5:06 # JavaScript (Node.js), 299 bytes f=(i,L=0,N=i.map((E,I)=>i.map(G=>I<G|0)).reverse(),Y=Q=>Q.map((e,j)=>Q.reduce((A,g,h)=>A+Q[h][j],0)),F=d=>i[N[L].indexOf(1)-d]&&i[N[L].lastIndexOf(1)-d]&&!(X=N.map((E,I)=>I>L?E:E.map((G,H)=>i[H+d]?E[H+d]:0))).some((E,I)=>E.some((G,H)=>i[I+1]&&G&!X[I+1][H]))&&X)=>F(1)?Y(F(1)):F(-1)?f(Y(F(-1)),L+1):i  Try it online! Yes, I know. Not the shortest. Not anywhere close. However, this answer actually performs the wiggling steps by moving layers left and right. Builds the list of layers, then attempts to move the current (L) layer to the left. If no layer is hovering in midair (some part is above empty air), then we return the resulting arrangement, otherwise we attempt to move to the right. If it is possible, recall f with incremented L, otherwise, because of the way the wiggling works, we are at the bottom layer and we simply return i unchanged. # Retina, 69 68 bytes \d+ * ^|$
,
V,?(_*),(?=(_+)(?!.*\2))(?<!\2.*)(_+\1,)+
^,|,$_+$.&


Try it online! Link includes test cases. Edit: Saved 1 byte by using @KevinCruijssen's approach. Explanation:

\d+
*
^|$,  Convert the unary and wrap the height map in an extra pair of ,s. V,?(_*),(?=(_+)(?!.*\2))(?<!\2.*)(_+\1,)+  Get the number preceding the largest number in the input (or 0 if it's the first number), and reverse the sublist of all numbers equal to or exceeding that number. (Excepting n,n-1,...,1, valid inputs always contain a sublist of the form k,n,n-1,...,k+1.) ^,|,$

_+
\$.&


Remove the extra ,s and convert to decimal.

# Charcoal, 37 bytes

≔⌕θ⌈θη≔⊕⌕θ⊕∧η§θ⊖ηζＩ…θ⊖ηＩ⮌✂θ∧η⊖ηζ¹Ｉ✂θζ


Try it online! Link is to verbose version of code. Explanation: Uses @KevinCruijssen's approach.

≔⌕θ⌈θη


Get the index of the largest element.

≔⊕⌕θ⊕∧η§θ⊖ηζ


Get the index of the successor of the element one greater than the predecessor of the largest element, or the length of the array (calculated as the index after the position of the 1 which is always last in that case) if the largest element is the first element.

Ｉ…θ⊖η


Output the elements up to the predecessor of the largest element.

Ｉ⮌✂θ∧η⊖ηζ¹


Output the elements not less than the predecessor of the largest element, but in reverse.

Ｉ✂θζ


Output the remaining elements.