Would this number make a good 2048 combo?

12

2

Inspired by xkcd.

Your challenge is to determine whether a number would make a good combination in the game 2048. Your input will be a number, such as:

8224

And the output will be whether that number would make a good 2048 combo, which for this input would be true or yes or 1 or any other way of indicating a positive result.

For those not familiar with the game, here's a simple explanation: powers of two are arranged on a grid, like this: [2] [2]. Tiles can be moved in any direction, and if two identical tiles meet, they become the next power of two (so [2] [2] when moved left or right becomes [4]). Or, you could just try the game here.

What does "a good 2048 combination" mean? It means any number that, if it was in the game "2048", it could be combined into one single number. (A zero means an empty space, and can be ignored if needed.) Note that numbers could possibly be multiple digits! However, the numbers must not change between moves. Here are some examples/test cases (with "Good" indicating a good combination, and "Bad" meaning not good):

  • Good: 8224 (8224 -> 844 -> 88 -> 16)
  • Good: 2222 (2222 -> 44 -> 8)
  • Good: 22048 (22048 -> 448 -> 88 -> 16)
  • Bad: 20482 (cannot combine the outer 2's, nor can you combine a 2048 and a 2)
  • Good: 20482048 (20482048 -> 4096)
  • Bad: 210241024 (210241024 -> 22048, but this is now [2] [2048] and cannot be combined since numbers cannot change between moves)
  • Good: 2048 (it's already one number)
  • Bad: 2047 (it's not a power of 2)
  • Bad: 11 (there are no 1's in the game)
  • Good: 000040000000 (zeroes are empty spaces)

Miscellaneous rules:

  • Input can be from anywhere reasonable, i.e. STDIN, function argument, file, etc.
  • Output can also be anywhere reasonable, i.e. STDOUT, function return value, file, etc.
  • Ignore the grid size - 22222222 should still output true.
  • The is no maximum to what s number could be, as long as it's a power of two. Therefore the possible numbers are any power of two greater than 0.
  • For those worried about zeroes causing ambiguity, that is not the case. For example, 22048 can be parsed as either [2] [2048] or [2] [2] [0] [4] [8]. The first one doesn't work, but the second one does, so it should output true.
  • This is , so the shortest code in bytes will win!

Doorknob

Posted 2014-03-20T04:13:40.043

Reputation: 68 138

2can I have a server supplying answer and just upload input download answer from it? total downloaded bytes will be 1 – Bryan Chen – 2014-03-20T04:30:38.120

"Bad: 11 (there are no 1's in the game)" There's no 4096 in the game, either. If it was me, I'd allow 1 in the input as 2^0. Not that it's a massive deal, but you know. – undergroundmonorail – 2014-03-20T04:44:59.820

@undergroundmonorail If 1 was legal you could parse 164 as 16 4 or 1 64. – Geobits – 2014-03-20T04:48:45.577

@Geobits Hm, I misread the input specs. You're correct, I wasn't accounting for multiple digit blocks. – undergroundmonorail – 2014-03-20T04:51:27.030

4@Geobits 2048 is already ambiguous as either one number or four. – John Dvorak – 2014-03-20T05:41:49.393

But yeah... the network rule is wrong. – John Dvorak – 2014-03-20T05:43:11.500

3A zero should not mean an empty space; is 1024 a legal number or not? Empty spaces should be unambiguous... and therefore having them at all does not contribute to the question, in my opinion. – Tal – 2014-03-20T07:18:20.217

7Your third example shows 22048 should output good but thats not true. You cant combine 2 with 2048 and the grid is 4x4 if all numbers should be seperate you'll get 5 cells. so maybe you should remove the 0? Also your 5th example seems to be invalid since the game stops at 2048 :) – Teun Pronk – 2014-03-20T08:43:48.500

Surely we can come up with new puzzles without constantly basing them on xkcd jokes? :) – Tim Seguine – 2014-03-20T10:24:46.640

@TimSeguine probably, but why would we? :P – Teun Pronk – 2014-03-20T10:32:27.590

1@TeunPronk touche – Tim Seguine – 2014-03-20T10:33:44.393

@m.b How? Does the game not end at 2048, as the name suggests? (As you can probably guess, I've never beaten it.) – undergroundmonorail – 2014-03-20T12:06:29.830

2@undergroundmonorail I can confirm there is a 4096 tile in the game. – Kendall Frey – 2014-03-20T12:14:02.010

2@undergroundmonorail, there is a "keep going"button when you reach 2048. – Martin Ender – 2014-03-20T12:46:41.033

@m.buettner Edited for clarity. – Doorknob – 2014-03-20T13:24:10.993

@Teun /cc ^. I never mentioned any restriction on grid size; apparently that was ambiguous, so I'll just state it explicitly. – Doorknob – 2014-03-20T13:25:02.137

@Doorknob ok but what is the max value we should work with? is it 4096 or higher? – Teun Pronk – 2014-03-20T13:28:03.300

@Teun z There is no maximum. I'll edit that in as well. – Doorknob – 2014-03-20T13:28:46.207

@hosch 20482048 could also be 8 tiles, but it wouldn't work that way, so it's assumed to be 2. – Doorknob – 2014-03-20T15:24:10.133

2Is "88222288888" valid. I ask because if you collapse to the left (like the game), this becomes 16,4,4,16,16,8->16,8,16,16,8 - which doesn't collapse further. But if you collapse lowest pairs first instead, this will collapse completely. (I think my solution probably has bugs to do with incomplete collapses, and collapsing to the right) – bazzargh – 2014-03-20T16:14:36.573

I think there's a bit of a problem in that every answer on this question seems to do something different :P – Tal – 2014-03-20T17:37:48.390

@Tal Well, yours violates the rules, so I'm not surprised that it's different from the others... – Doorknob – 2014-03-20T17:39:23.980

@bazzargh in your case, you could collapse to the right: 8 8 2 2 2 2 8 8 8 8 8 -> 16 4 4 8 16 16 -> 16 8 8 32 -> 16 16 32 -> 32 32 -> 64. But I don't doubt that it would be possible to come up with a case that would no longer work on the board but only by collapsing some inner pair first. – Martin Ender – 2014-03-20T17:51:46.700

@m.buettner fair point. I've fixed mine up to collapse simultaneously in pairs to left or right, exactly like the game. Turned out to need less code! – bazzargh – 2014-03-21T10:13:18.263

@m.buettner found a nice test case which can't be collapsed without cheating, by reflecting the previous example: "88888222288646488222288888". If you slide left, you get 8 32 8 16 128 64; or the reverse if you slide right; so it's not game-legal. But if you pick pairs to collapse, you can get 256. – bazzargh – 2014-03-21T10:32:12.590

1...and a much simpler one: "222244442222" – bazzargh – 2014-03-21T10:41:24.610

I'm not going to code, I'm going to keep playing this game! :D – Abbas – 2014-03-24T15:05:41.580

Answers

0

GolfScript, 137 characters

[0`]1$,4*,{2\)?`}%+:W;[]\]][]\{{:A;W{A 1=\?!},{[.A~@,>\@~+\].~!{0-.,/@+\0}*;}%~}%.}do+.{~}%,{{.-1%}%.&{[{.2$={+)}*}*]{1|(}%}%}*{,1=},!!

Input must be given on STDIN. The output is 0/1 for bad/good numbers. Most of the code is necessary for parsing the possible inputs.

This shorter version (113 characters) performs a simple shift test which would not work correctly for input like 224422.

[0`]1$,4*,{2\)?`}%+:W;[]\]][]\{{:A;W{A 1=\?!},{[.A~@,>\@~+\].~!{0-.,/@+\0}*;}%~}%.}do+{W{~[.:S]/[S.+]*}/,1=},!!

All test cases can be checked online.

Howard

Posted 2014-03-20T04:13:40.043

Reputation: 23 109

3

Python: 457 422 characters

z=range
def c(n):
 for x in z(1,12): 
  if int(n)==2**x:return 1
 return 0
def p(s):
 if s=='':return[]
 for i in z(len(s),0,-1):
  if c(s[:i])>0and p(s[i:])!=1:return [int(s[:i])]+p(s[i:])
 return 1
def r(a):
 if len(a)==1:return 1
 i,t=1,a[:1]
 while i<len(a):
  if a[i]==t[-1]:t[-1]*=2
  else:t+=[a[i]]
  i+=1
 if len(t)==len(a):return 0
 return r(t) 
def f(s):
 if p(s)==1or r(p(s))==0:print('bad')
 else:print('good')

The function f(s) gets a string of digits and outputs 'good' or 'bad' accordingly. I chose not to use 0 as spaces because spaces are meaningless in the game and they create ambiguity when parsing strings (is 22048 good or bad?). This only uses numbers up to 2048, but that can be changed without adding characters. At the cost of 10 characters or so I can also print all the steps of combining the numbers. And I realize that this code isn't golfed enough yet; don't worry, edits are coming.

Tal

Posted 2014-03-20T04:13:40.043

Reputation: 1 358

You can use the space and tab trick to save some characters on the indentation. SO markdown will break it though. – gcq – 2014-03-20T18:41:29.980

I think it doesn't work on Python 3.x. There's quite a lot I can do, but not sure I could compete with that Haskell answer :) – Tal – 2014-03-20T21:53:33.810

Yep, i forgot about that. – gcq – 2014-03-20T21:54:23.163

2

Haskell: 285 254 253 237 230 227

usage - just load it into ghci, and pass the string to h.

*Main> h "210241024"
False
*Main> h (replicate 1024 '2') -- very long string
True
*Main> h (replicate 1023 '2') -- odd one out
False

Code:

t=i 0
i n=mod n 2<1&&(n<3||i(div n 2))
a%[]|i a=[[a]]|t=[];a%(b:c)=[a:d|d<-b%c,i a]++(a*10+b)%c
c(0:a)=c a;c(a:b:d)|a==b=(a+a):c d|t=a:c(b:d);c a=a
l a=c a/=a&&(g.c)a
g[a]=t;g a=l a||(l.reverse)a
h b=any g$0%(map(read.(:[]))b)

Commentary: i is the check if a number is a power of 2, this will be outplayed by languages with bit twiddling. % recursively generates all parses that are lists of powers of 2 or 0. c collapses tiles. l recursively tests if tiles are collapsible left or good. g tests if tiles are collapsible left or right. There is no limit to the numbers on the tiles - eg h ((show (2^200))++(show (2^200))) returns true for 2 tiles marked "1606938044258990275541962092341162602522202993782792835301376".

Edited to fix a bug that it didn't correctly collapse "88222288888" to the right, but also found more golfing opportunities.

bazzargh

Posted 2014-03-20T04:13:40.043

Reputation: 2 476

2

Perl, 175-336 bytes

while(<>){chomp;$n="nothing";$\=("."x(1+/^[2048]+$/+/^((\d+)0*\2)+$/+((sprintf"%b",
$_)!~/1.*1/)))."\n";($o=$\)=~y/.\n/oh/;print$o;$m=length;for$i(1..$m){$a=$_;$r=
qr((\d*[2468])0*\2)0*/;($b=substr$a,0,$i,"")=~s/$r/$2+$2/ge;@n=$"="$b$a";push@n,$"while$"
=~s/$r/$2+$2/ge;($"%2&&next)||($">>=1)while$">1;$n="nice";(print)for@n;last}print$n}

Keeping just the essentials intact:

$_=shift;$m=length;for$i(1..$m){$a=$_;$r=qr/((\d*[2468])0*\2)0*/;($b=substr$a,0,$i,"")=~
s/$r/$2*2/ge;$"="$b$a";1while$"=~s/$r/$2*2/ge;($"%2&&next)||($">>=1)while$">1;exit}die;

1

ooh.. 1.. nice..

2

oooh... 2... nice...

22

oooh... 22... 4... nice...

42

ooh.. nothing..

422

ooh.. 422.. 44.. 8.. nice..

322

oh. nothing.

336

oh. nothing.

4224

ooh.. nothing..

4228

ooh.. 4228.. 448.. 88.. 16.. nice..

16022481602248

ooh.. 1604481602248.. 16088160448.. 1601616088.. 3216016.. 3232.. 64.. nice..

[64 and 256 lead to some poorly resolvable ambiguities that greedy matching can't cope with... but these are nice byte counts.]

2048

oooh... 2048... nice...

user130144

Posted 2014-03-20T04:13:40.043

Reputation: 303

1

Delphi 572582 characters

Edited code, limit is set to 2^30 so it wont exceed the MaxInt value in Delphi.

Golfed

uses SysUtils,Classes;var t,j,c:integer;s,n:string;L:TStringList;function p(x:string):boolean;var r,i:int64;begin if x='0'then exit(1>0);i:=2;r:=StrToInt(x);while i<r do i:=i*2;p:=i=r;end;begin read(s);t:=0;L:=TStringList.Create;j:=1;while j<=Length(s)do begin for c:=9downto 1do begin n:=copy(s,j,c);if p(n)then break;end;if n>'0'then L.Add(n);j:=j+Length(n);end;for j:=0to L.Count-1do t:=t+StrToInt(L[j]);j:=0;repeat if j=L.Count-1then break;if L[j]=L[j+1]then begin L[j]:=IntToStr(StrToInt(L[j])*2);L.Delete(j+1);j:=0;end else inc(j);until L.Count=1;write(strtoint(L[0])=t);end.

Ungolfed

uses
  SysUtils,
  Classes;

var
  t,j,c:integer;
  s,n:string;
  L:TStringList;
  function p(x:string):boolean;
  var
    r,i:int64;
  begin
    if x='0'then exit(1>0);
    i:=2;r:=StrToInt(x);
    while i<r do
      i:=i*2;
    p:=i=r;
  end;
begin
    read(s);
    t:=0;L:=TStringList.Create;
    j:=1;
    while j<=Length(s)do
    begin
      for c:=9downto 1do
      begin
        n:=copy(s,j,c);
        if p(n)then break;
      end;
      if n>'0'then L.Add(n);
      j:=j+Length(n);
    end;
    for j:=0to L.Count-1do
      t:=t+StrToInt(L[j]);
    j:=0;
    repeat
      if j=L.Count-1then break;
      if L[j]=L[j+1]then
      begin
        L[j]:=IntToStr(StrToInt(L[j])*2);
        L.Delete(j+1);j:=0
      end
      else
        inc(j);
    until L.Count=1;
    write(strtoint(L[0])=t);
end.

EDIT

So I got curious and wondered how many of these combinations would fit the puzzle and ran a test one it.

For others that are also curious, make a test too ;)

But ok here are the results:
20736 combinations were tested and 1166 were great combinations

I must say combinations with 3 or more zeroes were skipped (makes sense right?)
Combinations are almost unique, meaning the combinations 2248,8224,8422 and 4228 were all counted as a great combination.

Teun Pronk

Posted 2014-03-20T04:13:40.043

Reputation: 2 599

1

Mathematica - 218 bytes

f=MemberQ[DeleteCases[Map[FromDigits,l~Internal`PartitionRagged~#&/@Join@@Permutations/@IntegerPartitions@Length[l=IntegerDigits@#],{2}],{___,x_,___}/;!IntegerQ[2~Log~x]||x<2]//.{a___,x_,x_,b___}:>{a,2x,b},{_Integer}]&

Ungolfed version:

f[n_] := MemberQ[
  DeleteCases[
      Map[
        FromDigits, 
        Internal`PartitionRagged[l, #] & /@ 
          Join @@ Permutations /@ IntegerPartitions[Length[l = IntegerDigits[n]]], 
        {2}
      ],
      {___, x_, ___} /; x < 2 || ! IntegerQ[2~Log~x]
    ]
  ] //. {a___, x_, x_, b___} :> {a, 2 x, b}, 
  {_Integer}
]

The Internal\PartitionRagged` magic is taken from this question.

This solution handles arbitrary grid sizes and arbitrarily large numbers.

Here is a 195 byte version that works like the actual game with up to 4 tiles only (so f[22222222] is False):

f=MemberQ[(d=DeleteCases)[d[ReplaceList[IntegerDigits@#,{a__,b___,c___,d___}:>FromDigits/@{{a},{b},{c},{d}}],0,2],{___,x_,___}/;!IntegerQ[2~Log~x]||x<2]//.{a___,x_,x_,b___}:>{a,2x,b},{_Integer}]&

where I've replaced

Map[
  FromDigits, 
  Internal`PartitionRagged[l, #] & /@ 
    Apply[
      Join, 
      Permutations /@ IntegerPartitions[Length[l = IntegerDigits@#]]
    ], 
  {2}
]

with

ReplaceList[
  IntegerDigits[n], 
  {a__, b___, c___, d___} :> FromDigits /@ {{a}, {b}, {c}, {d}}
]

Martin Ender

Posted 2014-03-20T04:13:40.043

Reputation: 184 808

Just wondering if this has the same bug my code did - the DeleteCases looks like it removes leftmost pairs, so f[88222288888] would fail? – bazzargh – 2014-03-21T10:20:04.737

@bazzargh no, the DeleteCases just remove zeros and numbers that aren't power of two. The actual collapsing of pairs is done by the rule //. {a___, x_, x_, b___} :> {a, 2 x, b}, which works for that number and the reverse of it. I'm actually not entirely sure about the order Mathematica applies those replacements in, but it works. – Martin Ender – 2014-03-21T10:30:36.203

1

Haskell - 260 263

import Data.Bits
p[x]=[[[x]]]
p(x:s)=do r@(h:t)<-p s;[[x]:r,(x:h):t]
q=filter(and.map(\n->(n::Integer)/=1&&n.&.(-n)==n)).map(filter(/=0)).map(map read).p
c(x:y:s)
 |x==y=2*x:c s
 |True=x:(c$y:s)
c x=x
r[x]=True
r l=c l/=l&&(r(c l)||r(c$reverse l))
f=or.map r.q

f is the function. Examples:

> f"22228"
True
> f"20482044"
False

A little explanation:
p returns all ways to split a list.
q filters those that consist of only powers of 2 (excluding 1 but including 0).
c tries to collapse a string.
r iterates right and left collapsion until there's only 1 element left, or the string is incollapsable.

mniip

Posted 2014-03-20T04:13:40.043

Reputation: 9 396

Nice. There's a bug in c though, try "222244442222" - it returns true, but that's not collapsable in the game. Needs to recurse with (2*x):c s. – bazzargh – 2014-03-21T16:10:35.330

@bazzargh fixed – mniip – 2014-03-21T16:26:37.313