A game of 2048 goes like this:
- Every move you get another piece - either 2 90% of the time, or 4 10% of the time
- for each 2 equal pieces combined you get a piece of the sum of the two and the sum gets added to your score
Your task
given a board of a 4x4 game, give the average score of what A game like that would result in.
To calculate it, go the other way around of the game. For example, if there's an 8 piece, it's made from 4s. The average score of a 4 piece is 4*90% (90% of the time it's made from combining two 2 pieces together and it the other 10% a 4 isn't made, but ) = 3.6. As to make an 8 you need two 4s, and then you need to combine them to an 8, the result is 3.6*2 + 8 = 15.2.
In pseudocode a calculation for one number could be this:
function value (x) {
if (x > 4) return value(x / 2) * 2 + x
else if (x == 4) return 4*90%
else return 0
}
but you're free to choose any approach as long as it works the same.
Clarifications
- The input board will always be from a 4x4 board.
- You can take in the board as an array of the 16 values, as 16 different inputs or even a 2D array if that somehow helps you.
- The input numbers will always be zero or 2 to the power of a positive integer.
- You can discard any errors due to floating point operations (but theoretically the program must implement a correct algorithm).
- This is code-golf, the shortest code wins!
Test-cases
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] -> 0
[2,4,2,4,4,2,4,2,2,4,2,4,4,2,4,2] -> 28.8
[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4] -> 57.6
[0,0,0,4,0,0,0,8,0,0,0,16,2,2,0,2048] -> 20340.4
[2,16,2048,4096,4,32,1024,8192,8,64,512,16384,16,128,256,32768] -> 845465.2
[8,8,4,4,0,0,0,0,0,0,2,2,2048,65536] -> 996799.2
4
gets made from combining two2
pieces, while 10% of the time they appear as one and give no score \$\endgroup\$4
piece isn't always made from combining two2
's, as 10% of the time when a piece spawns in after a move, it starts as a4
\$\endgroup\$2
's one can only create4.5 4
's, not 9. Shouldn't the average score of a 4 piece be9/11 * 4 = 3.2727...
? \$\endgroup\$36
and an average score of36/11 = 3.2727...
\$\endgroup\$