-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCraneAlgorithm.java
183 lines (151 loc) · 13.3 KB
/
CraneAlgorithm.java
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
import java.util.*;
import java.io.*;
import java.math.*;
class Player {
public static String solve(int clawPos, int[] boxes, boolean boxInClaw) {
// Write your code here
// To debug: System.err.println("Debug messages...");
int numberOfPallets = boxes.length;
int numberOfBoxes = getTotalNumberOfBoxes(boxes, boxInClaw, numberOfPallets);
int[] sortedPalletArray = getSortedPalletArray(boxes, boxInClaw, numberOfPallets, numberOfBoxes);
int[] palletOffsetArray = getPalletOffsetArray(boxes, sortedPalletArray, numberOfPallets );
if(!boxInClaw){
String leastOffsetSide = getLeastOffsetSide(clawPos,palletOffsetArray);
int closestPositiveOffsetPostion = getClosestPositiveOffsetPostion(clawPos,palletOffsetArray,leastOffsetSide);
if(clawPos == closestPositiveOffsetPostion){
return "PICK";
} else if(clawPos > closestPositiveOffsetPostion){
return "LEFT";
} else {
return "RIGHT";
}
}
else{
String leastOffsetSide = getLeastOffsetSide(clawPos,palletOffsetArray);
int closestNegativeOffsetPostion = getClosestNegativeOffsetPostion(clawPos,palletOffsetArray,leastOffsetSide);
if(clawPos == closestNegativeOffsetPostion){
return "PLACE";
} else if(clawPos > closestNegativeOffsetPostion){
return "LEFT";
} else {
return "RIGHT";
}
}
}
public static String getLeastOffsetSide(int clawPos,int[] palletOffsetArray){
int leftOffsetNumber = 0;
int rightOffsetNumber = 0;
for(int i = 0; i < palletOffsetArray.length; i++){
if(palletOffsetArray[i] != 0){
if(i < clawPos){
leftOffsetNumber = leftOffsetNumber + 1;
} else if(i > clawPos){
rightOffsetNumber = rightOffsetNumber + 1;
}
}
}
if(leftOffsetNumber < rightOffsetNumber){
return "LEFT";
} else if(rightOffsetNumber < leftOffsetNumber) {
return "RIGHT";
} else {
return "";
}
}
public static int getClosestPositiveOffsetPostion(int clawPos,int[] palletOffsetArray, String leastOffsetSide) {
int closetPositveOffsetPosition = -1;
for(int i = 0; i < palletOffsetArray.length; i++){
int currentDistance = 0;
if(palletOffsetArray[i] > 0){
currentDistance = Math.abs(clawPos - i);
} else {
continue;
}
if((closetPositveOffsetPosition == -1) || (Math.abs(clawPos - closetPositveOffsetPosition) == currentDistance)){
if((leastOffsetSide.equals("RIGHT")) && (i > clawPos)) {
closetPositveOffsetPosition = i;
} else if((leastOffsetSide.equals("LEFT")) && (i < clawPos)) {
closetPositveOffsetPosition = i;
} else if((closetPositveOffsetPosition == -1)) {
closetPositveOffsetPosition = i;
}
} else if((closetPositveOffsetPosition == -1) || (Math.abs(clawPos - closetPositveOffsetPosition) > currentDistance)){
closetPositveOffsetPosition = i;
}
}
return closetPositveOffsetPosition;
}
public static int getClosestNegativeOffsetPostion(int clawPos,int[] palletOffsetArray, String leastOffsetSide) {
int closetNegativeOffsetPosition = -1;
for(int i = 0; i < palletOffsetArray.length; i++){
int currentDistance = 0;
if(palletOffsetArray[i] < 0){
currentDistance = Math.abs(clawPos - i);
} else {
continue;
}
if((closetNegativeOffsetPosition == -1) || (Math.abs(clawPos - closetNegativeOffsetPosition) == currentDistance)){
if((leastOffsetSide.equals("RIGHT")) && (i > clawPos)) {
closetNegativeOffsetPosition = i;
} else if((leastOffsetSide.equals("LEFT")) && (i < clawPos)) {
closetNegativeOffsetPosition = i;
} else if((closetNegativeOffsetPosition == -1)) {
closetNegativeOffsetPosition = i;
}
} else if((closetNegativeOffsetPosition == -1) || (Math.abs(clawPos - closetNegativeOffsetPosition) > currentDistance)){
closetNegativeOffsetPosition = i;
}
}
return closetNegativeOffsetPosition;
}
public static int getTotalNumberOfBoxes(int[] boxes, boolean boxInClaw, int numberOfPallets) {
int totalNumberOfBoxes = 0;
for(int i = 0; i < numberOfPallets;i++) {
totalNumberOfBoxes += boxes[i];
}
if(boxInClaw){
totalNumberOfBoxes++;
}
return totalNumberOfBoxes;
}
public static int[] getSortedPalletArray(int[] boxes, boolean boxInClaw, int numberOfPallets, int numberOfBoxes) {
int[] sortedPalletArray = new int[numberOfPallets];
int j = 0;
for(int i = 0; i < numberOfBoxes;i++) {
if(j == numberOfPallets){
j = 0;
}
sortedPalletArray[j] = sortedPalletArray[j] + 1;
j++;
}
return sortedPalletArray;
}
public static int[] getPalletOffsetArray(int[] boxes, int[] sortedPalletArray, int numberOfPallets) {
int[] palletOffsetArray = new int[numberOfPallets];
for(int i = 0; i < numberOfPallets;i++) {
palletOffsetArray[i] = boxes[i] - sortedPalletArray[i];
}
return palletOffsetArray;
}
/* Ignore and do not change the code below */
// #region main
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// game loop
while (true) {
int clawPos = in.nextInt();
boolean boxInClaw = in.nextInt() != 0;
int stacks = in.nextInt();
int[] boxes = new int[stacks];
for (int i = 0; i < stacks; i++) {
boxes[i] = in.nextInt();
}
PrintStream outStream = System.out;
System.setOut(System.err);
String action = solve(clawPos, boxes, boxInClaw);
System.setOut(outStream);
System.out.println(action);
}
}
// #endregion
}