8
1
Your task is to find the total destination of objects that are falling to a planet, you will get inputs like this:
7
...#...
.......
#..O..#
.......
...#...
.......
.......
where O is the planet and # are the objects, note that the planet will attract objects from the for basic directions(NEWS). You should output:
#
#O#
#
i.e the objects after reaching their destination.
The first number given as an input is the length of the grid(nxn), in the above example it is 7.
The object should always move in the shortest direction(note that the allowed directions are only horizontal and vertical; the object can't move diagonally)
If an object is equidistant from two directions it should go in a clock wise direction:
..#
...
O..
Will reach this side:
...
...
O#.
If an object collides with the planet, it will stop. If it collides with another object they become one object,
4
.#..
#...
....
.O..
would become:
....
....
.#..
.O..
One final example:
6
..#...
.#....
......
.#O...
....#.
......
Output:
......
......
..#...
.#O#..
......
......
Note: You should output the final shape, so all outputs are accepted as long as they show the final shape of the planet.
for example for this input:
...#
....
.O..
....
Should output:
O#
or
....
....
.O#.
....
Both of these are accepted.
This is code-golf so the shortest answer will win
Note 2:
As you may have noticed a .
represents vaccum so the shortest path is the shortest number of dots.
For example:
.#.
...
.O.
Is not accepted to be
O#
since the shortest path is vertical. Note3:
The object should always move to be in the same line with the planet in one of the four directions(choosing the shortest)
|<#
|
|
-----O------
|
Restrictions:
2<=n<=100
What are the possible input sizes? – undergroundmonorail – 2014-04-14T15:01:42.130
2Please clarify "shortest direction." – Ken A – 2014-04-14T15:50:37.617
@KenA I think it could be rephrased like: objects travel diagonally towards the planet until they are in line with the planet (after which they'll move straight towards the planet); if an object ends up touching the the planet diagonally, move one final step in counter-clockwise direction. This should lead to the same result as the OP's specification. But clarification by the OP would be nice. – Martin Ender – 2014-04-14T20:37:45.320
I noticed your example above has some trailing whitespace at the end of some lines. Is it safe to assume the same may be true of the input the program is expected to handle? – skibrianski – 2014-04-15T00:27:21.193
@skib no. It might be a typo but the inputs do not have trailing whitespace. – Mhmd – 2014-04-15T06:34:40.087
@Mhmd In your fourth example the result has an object on the left, right and top. In order to get the one at the right it moved up one row, which would be incorrect since that is counter clockwise, shouldnt it be at the bottom? – Teun Pronk – 2014-04-15T09:48:47.970
@TeunPronk no because it only has to move up by 1 unit, but would have to move to the left by 2 units. So the right side is closer to the object than the bottom. – Martin Ender – 2014-04-15T10:11:19.540
@m.buettner Yes but he has to move up 1 unit and then 1 unit to the left, which is still 2 steps – Teun Pronk – 2014-04-15T10:20:25.520
@TeunPronk hm, interesting. by this interpretation, every object that is not in line with the planet is equidistant from two final positions. I figured the OP meant that the object first moves to be in line with the planet along the shorter axis. But he should probably clarify that. – Martin Ender – 2014-04-15T10:31:07.090