PowerShell v2+, 81 bytes
param($n)($b=$n[$n.count..0]-join','-replace'(\d+),\1','($1*2)'|iex)[$b.count..0]
Takes input as an explicit array $n
, reverses it $n[$n.count..0]
, -join
s the elements together with a comma, then regex -replace
s a matching digit pair with the first element, a *2
, and surrounded in parens. Pipes that result (which for input @(2,2,4,4)
will look like (4*2),(2*2)
) over to iex
(short for Invoke-Expression
and similar to eval
), which converts the multiplication into actual numbers. Stores the resulting array into $b
, encapsulates that in parens to place it on the pipeline, then reverses $b
with [$b.count..0]
. Leaves the resulting elements on the pipeline, and output is implicit.
Test Cases
NB-- In PowerShell, the concept of "returning" an empty array is meaningless -- it's converted to $null
as soon as it leaves scope -- and so it is the equivalent of returning nothing, which is what is done here in the first example (after some wickedly verbose errors). Additionally, the output here is space-separated, as that's the default separator for stringified arrays.
PS C:\Tools\Scripts\golfing> @(),@(2,2,4,4),@(2,2,2,4,4,8),@(2,2,2,2),@(4,4,2,8,8,2),@(1024,1024,512,512,256,256),@(3,3,3,1,1,7,5,5,5,5)|%{"$_ --> "+(.\2048-like-array-shift.ps1 $_)}
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string.
At C:\Tools\Scripts\golfing\2048-like-array-shift.ps1:7 char:67
+ param($n)($b=$n[$n.count..0]-join','-replace'(\d+),\1','($1*2)'|iex)[$b.count. ...
+ ~~~
+ CategoryInfo : InvalidData: (:String) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Cannot index into a null array.
At C:\Tools\Scripts\golfing\2048-like-array-shift.ps1:7 char:13
+ param($n)($b=$n[$n.count..0]-join','-replace'(\d+),\1','($1*2)'|iex)[$b.count. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
-->
2 2 4 4 --> 4 8
2 2 2 4 4 8 --> 2 4 8 8
2 2 2 2 --> 4 4
4 4 2 8 8 2 --> 8 2 16 2
1024 1024 512 512 256 256 --> 2048 1024 512
3 3 3 1 1 7 5 5 5 5 --> 3 6 2 7 10 10
11This is a very nice first challenge. Welcome to the site! – James – 2016-10-05T20:12:44.063
Related. Related. (There are several other 2048-based challenges, but the merging operation is most important in these two I think.) – Martin Ender – 2016-10-05T20:49:04.197
1Input is not necessarily sorted and numbers are greater than zero, that is the only restriction on numbers. We may let largest value fit in standard int32 bounds i think. Empty array gives empty array as a result. Thanks for the participation, appreciate that :) – greenwolf – 2016-10-06T01:52:07.683
3To those still voting to close as unclear, the challenge essentially boils down to this: Assume you have an array of positive integers. Walk through it from end to start. If the current element is equal to the next, replace it with the sum of both and move to the element after the replacement, then perform this check again for that element and the next. Repeat until the beginning of the array is reached. – user2428118 – 2016-10-06T09:49:31.897
Is
[4,2,8,8]
valid output for[2,2,2,4,4,8]
? – Titus – 2016-10-06T10:05:05.6701@Titus "Notice that if we have 3 consecutive equal elements, we have to sum rightmost ones, so for example, [2, 2, 2] should become [2, 4], not [4, 2]." – Martin Ender – 2016-10-06T10:38:41.220
1The ruling on empty arrays is unfortunate; it has invalidated a few answers, including my own. – Dennis – 2016-10-06T14:31:35.317
1I'm guessing APL would be a good language for writing a 2048 game. – Joe – 2016-10-06T20:46:49.430
Can we assume that the array length is always even, like in your examples? – Bergi – 2016-10-07T13:01:48.373
No, it can be odd also. I wonder about an approach in which it will make a difference, could you share? :) – greenwolf – 2016-10-07T17:59:36.810
I think the answers that use string processing rather than an array should be disqualified, unless they first serialise the array, then string process it, then deserialise it again. – Alnitak – 2016-10-10T13:46:48.163
@Joe Correct.
– Adám – 2018-12-25T11:58:53.407