5 kyu

Simple Fun #348: Rotate Clocks

63 of 75myjinxin2015

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 element n(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

Algorithms

Stats:

CreatedSep 20, 2017
PublishedSep 20, 2017
Warriors Trained225
Total Skips10
Total Code Submissions375
Total Times Completed75
JavaScript Completions63
Python Completions15
Total Stars8
% of votes with a positive feedback rating92% of 31
Total "Very Satisfied" Votes26
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes0
Total Rank Assessments3
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • Voile Avatar
  • dfhwze Avatar
Ad