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 code-golf, so the shortest code in bytes will win!
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 outputgood
but thats not true. You cant combine2
with2048
and the grid is4x4
if all numbers should be seperate you'll get 5 cells. so maybe you should remove the0
? Also your 5th example seems to be invalid since the game stops at2048
:) – Teun Pronk – 2014-03-20T08:43:48.500Surely 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