SHOW:
|
|
- or go back to the newest paste.
1 | namespace _12_StringMatrixRotation | |
2 | { | |
3 | using System; | |
4 | using System.Collections.Generic; | |
5 | using System.Linq; | |
6 | using System.Text.RegularExpressions; | |
7 | ||
8 | class StringMatrixRotation | |
9 | { | |
10 | static void Main() | |
11 | { | |
12 | var matrix = new List<Queue<char>>(); | |
13 | ||
14 | var rotations = new Queue<int>(); | |
15 | var rotRegex = new Regex(@"(\d+)"); | |
16 | ||
17 | // parse input | |
18 | while (true) | |
19 | { | |
20 | var cmd = Console.ReadLine(); | |
21 | ||
22 | if(cmd == "END") | |
23 | { | |
24 | break; | |
25 | } | |
26 | ||
27 | if (rotRegex.IsMatch(cmd)) | |
28 | { | |
29 | var degree = rotRegex.Match(cmd).Groups[1].Value; | |
30 | rotations.Enqueue(int.Parse(degree)); | |
31 | } | |
32 | else | |
33 | { | |
34 | matrix.Add(new Queue<char>(cmd.ToCharArray())); | |
35 | } | |
36 | } | |
37 | ||
38 | // extend queues | |
39 | var width = matrix.Max(s => s.Count); | |
40 | var height = matrix.Count; | |
41 | ||
42 | foreach (var q in matrix) | |
43 | { | |
44 | if(q.Count < width) | |
45 | { | |
46 | while (q.Count != width) | |
47 | { | |
48 | q.Enqueue(' '); | |
49 | } | |
50 | } | |
51 | } | |
52 | ||
53 | // compute target rotation | |
54 | var totalRotation = rotations.Sum(); | |
55 | var degreeReach = (totalRotation >= 360) ? totalRotation % 360 : totalRotation; | |
56 | ||
57 | var quarterRotations = degreeReach / 90; | |
58 | ||
59 | // apply rotation | |
60 | bool compareWidth = true; | |
61 | for (int i = 0; i < quarterRotations; i++) | |
62 | { | |
63 | var buffer = new List<Queue<char>>(); | |
64 | for (int c = 0; c < ((compareWidth) ? width : height); c++) | |
65 | { | |
66 | var q = new Queue<char>(); | |
67 | ||
68 | for (int r = matrix.Count - 1; r >= 0; r--) | |
69 | { | |
70 | var symbol = matrix[r].Dequeue(); | |
71 | q.Enqueue(symbol); | |
72 | } | |
73 | buffer.Add(q); | |
74 | } | |
75 | ||
76 | matrix = buffer; | |
77 | ||
78 | compareWidth = !compareWidth; | |
79 | } | |
80 | ||
81 | ||
82 | foreach (var item in matrix) | |
83 | { | |
84 | while (item.Count > 0) | |
85 | { | |
86 | Console.Write(item.Dequeue()); | |
87 | } | |
88 | Console.WriteLine(); | |
89 | } | |
90 | } | |
91 | } | |
92 | } |