1
\$\begingroup\$

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]
\$\endgroup\$
  • \$\begingroup\$ Suggested additional test case: [2,0,2,2] \$\endgroup\$ – Adám Nov 28 at 2:34
  • \$\begingroup\$ 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 "ЀЀ @". \$\endgroup\$ – Adám Nov 28 at 2:37
  • 1
    \$\begingroup\$ hmm, dont think we should allow that. just straight integers numbers is fine \$\endgroup\$ – xiaodai Nov 28 at 2:43
1
\$\begingroup\$

J, 31 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

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

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
\$\endgroup\$
  • 3
    \$\begingroup\$ I recommend leaving your challenge open for a week before answering it yourself. \$\endgroup\$ – Adám Nov 28 at 2:23
  • 2
    \$\begingroup\$ There's no reason to make this answer a community wiki. \$\endgroup\$ – Adám Nov 28 at 2:24
  • 6
    \$\begingroup\$ 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. \$\endgroup\$ – Adám Nov 28 at 2:27
  • 5
    \$\begingroup\$ Bad answers (as in clearly not golfed) are actually not allowed. \$\endgroup\$ – Adám Nov 28 at 2:32
  • 1
    \$\begingroup\$ Maybe this can help you? \$\endgroup\$ – Adám Nov 28 at 2:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.