Simple Fun #348: Rotate Clocks
Description:
Here are nine clocks on the wall. They are arranged into a 3x3 matrix. like this:
+-------+ +-------+ +-------+
| | | | | | |
|---O | |---O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O | | O |
| | | | | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O---| | O |
| | | | | | | |
+-------+ +-------+ +-------+
As you can see, the hour hand of these clocks always point to 12 o'clock, 3 o'clock, 6 o'clock or 9 o'clock.
We can dial the hour hand of the clock. Each time you dial it, you can turn the clock clockwise by 90 degrees.
The hour hand of the clock doesn't turn alone, some of them always rotate at the same time. Use the following 9 methods:
Let's named 9 clocks A,B,C,D,E,F,G,H,I
A,B,C
D,E,F
G,H,I
method 1: ABC (3 clocks of row1 rotate at the same time)
method 2: GHI (3 clocks of row3 rotate at the same time)
method 3: ADG (3 clocks of column1 rotate at the same time)
method 4: CFI (3 clocks of column3 rotate at the same time)
method 5: ABDE (4 clocks of up-left rotate at the same time)
method 6: BCEF (4 clocks of up-right rotate at the same time)
method 7: DEGH (4 clocks of down-left rotate at the same time)
method 8: EFHI (4 clocks of down-right rotate at the same time)
method 9: BDEFH (5 clocks of + shape at center rotate at the same time)
Task
You are given two arguments:
clocks
: a string like the example above represents the initial state of 9 clocks. Lines are separated by\n
and are of equal length, including the empty lines.turns
: an positive integer array. each elementn
(1-9) in the array represents the number of method.
Your task is following turns
to rotate clocks
. return the final state of clocks.
Constraints
- 100 random tests, of which the biggest have cases have
100,000
turns.
Examples
For clocks =
+-------+ +-------+ +-------+
| | | | | | |
|---O | |---O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O | | O |
| | | | | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O---| | O |
| | | | | | | |
+-------+ +-------+ +-------+
,turns = [3,9,2,8]
The output should be:
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
The process:
initial clocks:
+-------+ +-------+ +-------+
| | | | | | |
|---O | |---O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O | | O |
| | | | | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
| O | | O---| | O |
| | | | | | | |
+-------+ +-------+ +-------+
After using method 3, ADG rotate 90 degrees:
+-------+ +-------+ +-------+
| | | | | | | |
| O | |---O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
|---O | | O | | O |
| | | | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
|---O | | O---| | O |
| | | | | | |
+-------+ +-------+ +-------+
After using method 9, BDEFH rotate 90 degrees:
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | |
| O | |---O | |---O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | |
|---O | | O | | O |
| | | | | | | |
+-------+ +-------+ +-------+
After using method 2, GHI rotate 90 degrees:
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | |
| O | |---O | |---O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | |
| O | |---O | |---O |
| | | | | |
+-------+ +-------+ +-------+
After using method 8, EFHI rotate 90 degrees:
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
+-------+ +-------+ +-------+
| | | | | | | | |
| O | | O | | O |
| | | | | |
+-------+ +-------+ +-------+
Note
All inputs are valid.
In order to avoid timeout, be aware of the code's performance ;-)
For more challenge, please try Challenge Fun: Rotate Clocks(Coming soon)
Happy Coding
^_^
Similar Kata:
Stats:
Created | Sep 20, 2017 |
Published | Sep 20, 2017 |
Warriors Trained | 225 |
Total Skips | 10 |
Total Code Submissions | 375 |
Total Times Completed | 75 |
JavaScript Completions | 63 |
Python Completions | 15 |
Total Stars | 8 |
% of votes with a positive feedback rating | 92% of 31 |
Total "Very Satisfied" Votes | 26 |
Total "Somewhat Satisfied" Votes | 5 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 3 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |