-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_of_the_rings.java
More file actions
197 lines (171 loc) · 5.08 KB
/
code_of_the_rings.java
File metadata and controls
197 lines (171 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.util.*;
import java.io.*;
import java.math.*;
class Player {
private static char [] charTab = new char[27];
private static char runeCourante = ' ';
private static char [] runes = new char[30];
private static int [] distLeft = new int[30];
private static int [] distRight = new int[30];
private static String magicPhrase;
private static int courant = 0;
private static int indexL = 30, indexR = 30;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
magicPhrase = in.nextLine();
String instruction = "";
initAll();
for(int i = 0; i < magicPhrase.length(); i++){
computeDist(i);
int minL = minDistLeftIndex(), minR = minDistRightIndex();
if(minL < minR){
if(leftDistanceRunes(courant, indexL) < rightDistanceRunes(courant, indexL)){
for(int j = 0; j < leftDistanceRunes(courant, minL) - 1; j++){
instruction += '<';
}
}
else{
if(leftDistanceRunes(courant, indexL) != 0 && rightDistanceRunes(courant, indexL) != 0){
for(int j = 0; j < rightDistanceRunes(courant, indexL) - 1; j++){
instruction += '>';
}
}
}
runeCourante = runes[indexL];
courant = indexL;
for(int j = 0; j < leftDistance(runeCourante, magicPhrase.charAt(i)); j++){
instruction += '+';
}
runes[indexL] = magicPhrase.charAt(i);
runeCourante = magicPhrase.charAt(i);
courant = indexL;
}
else{
if(leftDistanceRunes(courant, indexR) < rightDistanceRunes(courant, indexR)){
for(int j = 0; j < leftDistanceRunes(courant, indexR) - 1; j++){
instruction += '<';
}
}
else{
if(leftDistanceRunes(courant, indexR) != 0 && rightDistanceRunes(courant, indexR) != 0){
for(int j = 0; j < rightDistanceRunes(courant, indexR)- 1; j++){
instruction += '>';
}
}
}
runeCourante = runes[indexR];
courant = indexR;
for(int j = 0; j < rightDistance(runeCourante, magicPhrase.charAt(i)); j++){
instruction += '-';
}
runes[indexR] = magicPhrase.charAt(i);
runeCourante = runes[indexR];
courant = indexR;
}
instruction += '.';
}
System.out.println(instruction);
}
public static void initAll(){
charTab[0] = ' ';
for(int i = 1; i < charTab.length; i++){
charTab[i] = (char) (65 + i - 1);
}
for(int i = 0; i < runes.length; i++){
runes[i] = ' ';
}
}
public static int charIndex(char c){
for(int i = 0; i < charTab.length; i++){
if(charTab[i] == c)
return i;
}
return -1;
}
public static int leftDistance(char c1, char c2){
int d = 0;
for(int i = charIndex(c2); i >= 0; i--){
if(charIndex(c1) == i)
return d;
d++;
}
for(int i = 26; i >= 0; i--){
if(charIndex(c1) == i)
return d;
d++;
}
return d;
}
public static int rightDistance(char c1, char c2){
int d = 0;
for(int i = charIndex(c2); i < 27; i++){
if(charIndex(c1) == i)
return d;
d++;
}
for(int i = 0; i < charIndex(c2); i++){
if(charIndex(c1) == i)
return d;
d++;
}
return d;
}
public static int leftDistanceRunes(int c1, int c2){
int d = 0;
for(int i = c2; i >= 0; i--){
if(c1 == i)
return d;
d++;
}
for(int i = 29; i >= 0; i--){
if(c1 == i)
return d;
d++;
}
return d;
}
public static int rightDistanceRunes(int c1, int c2){
int d = 0;
for(int i = c2; i < 30; i++){
if(c1 == i)
return d;
d++;
}
for(int i = 0; i < c2; i++){
if(c1 == i)
return d;
d++;
}
return d;
}
public static void computeDist(int k){
for(int i = 0; i < distLeft.length; i++){
distLeft[i] = leftDistance(runes[i], magicPhrase.charAt(k)) +
(leftDistanceRunes(courant, i) < rightDistanceRunes(courant, i)
?leftDistanceRunes(courant, i):rightDistanceRunes(courant, i));
distRight[i] = rightDistance(runes[i], magicPhrase.charAt(k)) +
(leftDistanceRunes(courant, i) < rightDistanceRunes(courant, i)
?leftDistanceRunes(courant, i):rightDistanceRunes(courant, i));
}
}
public static int minDistLeftIndex(){
int min = 31;
for(int i = 0; i < distLeft.length; i++){
if(min > distLeft[i]){
min = distLeft[i];
indexL = i;
}
}
return min;
}
public static int minDistRightIndex(){
int min = 31;
for(int i = 0; i < distRight.length; i++){
if(min > distRight[i]){
min = distRight[i];
indexR = i;
}
}
return min;
}
}