Triangular Key
InterviewTime limit2sMemory limit1024 MB
Rotate a triangular grid of digits 90 degrees in the given direction and print the result in the same triangular format.
- Level
Medium4 of 10
- Topics
- Implementation, Matrix
- Solved
- No attempts yet
Problem
Indiana Jones has a key to the door that leads to the Inca's hidden treasure. The key is a regular triangle divided into small regular triangles, and each small triangle has one decimal digit written in it. The key is shown in the figure below.

Jones fit the triangle into the matching triangular hole in the door, and the door did not open. He may have inserted the key incorrectly, since it can be inserted in three ways. Jones has only one attempt left. If he fails, the Inca warriors chasing him will kill him. He wants to see how the key looks when inserted the other two ways, and then he will choose the right way. You are one of his two assistants, and he asks you to show him only one of the two possible variants. The key above, rotated counterclockwise and clockwise, is shown in the figure below.

If you want Indiana to survive and want your share of the treasure, you have to help him!
Input
The first line contains one integer (). The next lines describe the triangular key. Line contains digits, which are the contents of the -th row of the key.
The last line contains the word counterclockwise if the triangle must be rotated counterclockwise, and clockwise otherwise.
Output
Print the key after rotating it in the required direction. Describe the triangle in the same format as the input.