14
1
Introduction
You're playing a matching game, in which coins are inserted at the top and fall to the bottom (onto the top coin) due to gravity.
So this
O <- inserting this coin
OO O
OOOOO
will become this
O
OO O
OOOOO
Now imagine someone rotates the board clockwise. The following will happen:
1. The board is rotated
OOO
OO
O
OO
O
2. Coins fall down due to gravity
O
O
OO
OO
OOO
Your task
Your task is to simulate the rotation of the board by writing a program or a function. For the sake of simplicity we're only dealing with one kind of coin (it's not a too exciting matching game, is it…). You can assume that gravity is applied only after the rotation is complete. The board is rotated clockwise.
Input
The input is going to be a string, which contains 3 types of characters:
- O (capital o) OR 0 (zero) - a coin (you decide which one your solution supports)
- (space) - an empty field
- \n (new line) - end of row
The input represents the state of the board. You can assume, the input is well formed and contains a valid state of the board (no coins are floating). The input can be a function parameter, or can be read from the standard input or from a file.
Output
The output is the new state of the board after rotation. The output contains the same 3 types of characters as the input. The output can be returned from your function or can be written to the standard output or to a file.
Sample
Input1:
O
OO O
OOOOO
Output1:
O
O
OO
OO
OOO
Input2:
O O
O O
Output2:
OO
OO
You can use any language and the standard library of the chosen language. Shortest program in bytes wins.
Are the shorter lines padded with trailing spaces? – Ventero – 2014-06-26T15:05:22.060
If you need so then yes. – David Frank – 2014-06-26T15:08:56.763
What are the requirements for the board size? Can I choose a reasonable maximum size, or does the application/function need to work for all possible sizes? – Fors – 2014-06-26T16:16:05.473
You can assume that the maximum size of the board is 50x50 – David Frank – 2014-06-26T16:17:50.083
Does any part of the task involve matching? – user2357112 supports Monica – 2014-06-26T18:19:17.040
@user2357112 no – David Frank – 2014-06-26T18:22:10.420
2If gravity is applied after rotation, how does Input2 become Output2? I would have thought it would drop the top coins down but not horizontally? – Matt – 2014-06-26T23:37:44.053
I think the correct output2 should be two piles of two coins separated by one empty column. – justhalf – 2014-06-27T02:34:11.267
2@Matt Note, that there are no empty rows in Input2, nor in Output2 (SE displays margin between the rows). – David Frank – 2014-06-27T10:35:59.883
@justhalf
You can assume, the input is well formed and contains a valid state of the board (no coins are floating)
It's actually impossible to get a blank column sandwiched between non-blank columns – Cruncher – 2014-06-27T15:33:04.310Yes, I thought there was an empty row in the input. – justhalf – 2014-06-27T16:09:02.547
@David Frank, you are right, makes sense now! The empty column along with what looked like the empty row was confusing (to me!). – Matt – 2014-06-29T23:12:31.227