# Simulate move left for 2048 [duplicate]

I have a fixed length vector of 4 elements. The inputs of which are always 0 or 2^n for n = 1 to 15 (so n = 0 excluded i.e. 2^0 = 1 will not appear)

x = [32, 4, 4, 8]

the result should be

x = [32, 8, 8, 0]

The way it works is that values that are the same will get combined, but one it's combined it can not be combined with another value for another turn.

and we want to simulate the shortest code to make simulate 2048 move left on this one row.

The shortest code wins! It's fixed-size 4 vector. So it's not the same as the other problem. Also, you need to put zeros here.

Test cases:

[0,0,0,0] -> [0,0,0,0]
[0,0,0,2] -> [2,0,0,0]
[2,0,0,4] -> [2,4,0,0]
[8,0,0,8] -> [16,0,0,0]
[16,16,32,64] -> [32,32,64,0]
[16,16,8,8] -> [32,16,0,0]
[2,2,2,0] -> [4,2,0,0]

• Suggested additional test case: [2,0,2,2] – Adám Nov 28 at 2:34
• May we take and give the vector as a four-character string of Unicode characters with those code points? E.g. [1024,1024,32,64] as "ЀЀ @". – Adám Nov 28 at 2:37
• hmm, dont think we should allow that. just straight integers numbers is fine – xiaodai Nov 28 at 2:43

# J, 31 bytes

4\$!.0[:;_2([:<,+@.=/)\ ::0-.&0


Try it online!

# APL (Dyalog Unicode), 36 bytesSBCS

Anonymous tacit prefix function.

⎕UCS{4↑⍺⍺t⎕R(1⌽∪¨t←2/¨⍺⍺2*⍳16)⍺⍺⍵~0}


Try it online!

⎕UCS{} derived function where ⍵ is the argument and ⍺⍺ converts to/from code points:

⍵~0 the argument without zeros

⍺⍺ convert to code points

t⎕R()Replace the texts t (defined below) with the corresponding texts from:

⍳16 the indices from 1 to 16

2* two raised to the power of those

⍺⍺ convert them to characters

2/¨ make two of each

t← assign to t

∪¨ get the unique element from each (i.e. deduplicate them)

1⌽ cyclically rotate the first string to the end

⍺⍺ convert to code points

4↑ take the first four elements, padding with zeros

• You need to remove zeros before merging the numbers, and the grouping direction is also different. – Bubbler Nov 28 at 3:48
• @Bubbler OK, fixed now. Thanks for letting me know. – Adám Nov 28 at 10:25

Julia 277 248 bytes

m(x)=begin
t=true
e=falses(3)
for i in 2:4
if x[i]!=0
d=!t
for j in i-1:-1:1
if x[j]!=0
if(x[j]!=x[i])|e[j]
p=x[i]
x[i]=0
x[j+1]=p
d=t
break
elseif x[j]==x[i]
x[j],x[i]=x[i]*2,0
e[j]=t
d=t
break
end
end
end
if !d
x[1],x[i]=x[i],0
end
end
end
x
end
`
• I recommend leaving your challenge open for a week before answering it yourself. – Adám Nov 28 at 2:23
• There's no reason to make this answer a community wiki. – Adám Nov 28 at 2:24
• I'm sorry you take it like that. I'm just trying to guide you towards what's normal on this site. In fact, you'll find that many challenges have multiple answers in the same language. – Adám Nov 28 at 2:27
• Bad answers (as in clearly not golfed) are actually not allowed. – Adám Nov 28 at 2:32