-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYodleJuggle.java
591 lines (502 loc) · 12.7 KB
/
YodleJuggle.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* JuggleFest problem for Yodle
*
* @author Amit Ruparel / [email protected]
* @version 1.0
*
*/
import java.io.*;
import java.util.*;
/**
* This class represents the Circuit objects
* @author amit
*/
class Circuit
{
private int h;
private int e;
private int p;
private int id;
private TreeSet<Juggler> ts;
private int capacity;
/**
* Constructs a new Circuit object with values of hand to eye coordination (H), endurance (E) and pizzazz (P)
* @param h Hand to eye coordination
* @param e Endurance
* @param p Pizzazz
* @param id Circuit identification
*/
public Circuit(int h,int e,int p,int id)
{
this.h=h;
this.e=e;
this.p=p;
this.id=id;
this.ts=new TreeSet<Juggler>(new JugglerComparator(this));
}
/**
* Sets the max number of jugglers that can be assigned to the circuit
* @param size The maxsize
*/
public void setSize(int size)
{
capacity=size;
}
/**
* Returns the score of the match between this circuit and given juggler
* @param j1 Given juggler
* @return the score of the match
*/
public int matchScore(Juggler j1)
{
return h*j1.getH()+p*j1.getP()+e*j1.getE();
}
/**
* Get the Jugglers assigned to this circuit in an ArrayList
* @return ArrayList of Juggler objects
*/
public ArrayList<Juggler> getJugglerList()
{
return (new ArrayList<Juggler>(ts.descendingSet()));
}
/**
* Tries to assign a given juggler to this circuit and returns value accoring to result of the trial
* @param j Given juggler
* @return 0 if successful, value of juggler replaced if some juggler was replaced, -1 if attempt was unsuccessful
*/
public int attemptAdd(Juggler j)
{
if(this.ts.size()<capacity)
{
this.ts.add(j);
return 0;
}
else if(matchScore(this.ts.first())<matchScore(j))
{
int removedId=this.ts.first().getid();
this.ts.remove(this.ts.first());
this.ts.add(j);
return removedId;
}
else
return -1;
}
/**
* Returns string represenation of the circuit
*/
public String toString()
{
return String.format("Circuit h=%d, e=%d, p=%d \n %s",h,e,p,ts.toString());
}
/**
* Check if this circuit can be assigned anymore jugglers
* @return true if another juggler can be assigned to this circuit
*/
public boolean isFull()
{
return ts.size()==capacity;
}
/**
* Print the juggler assignments for a given circuit in the required form
* @param clist ArrayList of Circuit objects
*/
public void printAsssignments(ArrayList<Circuit> clist)
{
ArrayList<Juggler> jugglerList=new ArrayList<Juggler>(ts.descendingSet());
System.out.print("C"+id+" ");
boolean first=true;
for(Juggler j:jugglerList)
{
if(first)
{
first=false;
System.out.print(j.prefString(clist));
}
else
System.out.print(", "+j.prefString(clist));
}
System.out.println();
}
/**
* Get the id of this circuit
* @return id
*/
public int getId()
{
return id;
}
}
class Juggler
{
int id;
private int h;
private int e;
private int p;
private int pref[];
/**
* Constructs a new Juggler object with values of hand to eye coordination (H), endurance (E) and pizzazz (P)
* @param h Hand to eye coordination
* @param e Endurance
* @param p Pizzazz
* @param id Juggler identification number
* @param prefCapacity The number of circuit preferences that a juggler can have
*/
public Juggler(int h,int e,int p,int id,int prefCapacity)
{
this.h=h;
this.e=e;
this.p=p;
this.id=id;
pref=new int[prefCapacity];
}
/**
* Sets the preference array of this juggler
* @param pref the preference array to be set
* @return This same juggler object
*/
public Juggler setPref(int[] pref)
{
this.pref=pref;
return this;
}
/**
* Get the value for Pizzaz
* @return Pizzaz value
*/
public int getP() { return p; }
/**
* Get the value for Endurance
* @return Endurance value
*/
public int getE() { return e; }
/**
* Get the value for Hand to Eye coordination
* @return Hand to eye coordination value
*/
public int getH() { return h; }
/**
* Get the id number for the juggler
* @return Identification number
*/
public int getid() { return id; }
/**
* Get the i'th preferred circuit of this juggler
* @param i Representing which circuit for this juggler we want
* @return i'th circuit
*/
public int getPref(int i)
{
return pref[i];
}
/**
* Gets the number of preferred circuits this juggler can have
* @return The number of preferred circuits
*/
public int getPrefSize()
{
return pref.length;
}
/**
* Retusnt the string representation
*/
public String toString()
{
return String.format("J%d",id);
}
/**
* Returns the string representation of the preferences of this juggler with their scores in required form
* @param clist The ArrayList containing all the Circuit objects
* @return String representation of preferred circuits with scores
*/
public String prefString(ArrayList<Circuit> clist)
{
String retString="J"+id;
Circuit[] cArr=new Circuit[pref.length];
for(int i=0;i<pref.length;i++)
{
cArr[i]=clist.get(pref[i]);
}
//Arrays.sort(cArr,Collections.reverseOrder(new CircuitComparator(this))); ///HEREEE
for(int i=0;i<pref.length;i++)
{
retString+=" C"+cArr[i].getId()+":"+cArr[i].matchScore(this);
}
return retString;
}
@Override
public boolean equals(Object jnew)
{
if(!(jnew instanceof Juggler))
return false;
return this.id==((Juggler)jnew).id;
}
@Override
public int hashCode()
{
return this.id;
}
}
/**
* This class will be used to compare and arrange various Juggler objects, given a particular circuit
* @author amit
*
*/
class JugglerComparator implements Comparator<Juggler>
{
private Circuit c;
/**
* Creates a new JugglerComparator initialized with a given Circuit with whom the jugglers' skills must be matched to be compared
* @param c Circuit of reference for comparision of Jugglers
*/
public JugglerComparator(Circuit c)
{
this.c=c;
}
/**
Return 1 if Juggler j1 is a better match for circuit c, 0 if both are equal matches and -1 is j2 is a better match
*/
public int compare(Juggler j1, Juggler j2)
{
int compareScore= (new Integer(c.matchScore(j1))).compareTo(new Integer(c.matchScore(j2)));
if(compareScore==0)
{
return j1.getid()==j2.getid()?0:-1;
}
else
return compareScore;
}
}
/**
* Main class that uses all the other classes
* @author amit
*
*/
public class YodleJuggle
{
/**
* Main method that carries the execution
* @param arg Not used
*/
public static void main(String arg[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
ArrayList<Circuit> clist=new ArrayList<Circuit>();
ArrayList<Juggler> jlist=new ArrayList<Juggler>();
int circuitCount=loadCircuits(br,clist);
int jugglerCount=loadJugglers(br,jlist);
for(Circuit c:clist)
c.setSize(jugglerCount/circuitCount);
int[] whereplaced=matchMaker(clist,jlist);
for(int i=clist.size()-1;i>=0;i--)
{
clist.get(i).printAsssignments(clist);
}
}
catch(IOException e)
{
System.out.println("There was some problem with I/O stream. Details:"+e);
e.printStackTrace();
}
}
/**
* Matches all the Juggler objects to appropriate Circuit objects as required
* @param clist ArrayList of Circuit objects
* @param jlist ArrayList of Juggler Objects
*/
/*public static void matchMaker(ArrayList<Circuit> clist,ArrayList<Juggler> jlist)
{
int currPositions[]=new int[jlist.size()];
boolean settled[]=new boolean[jlist.size()];
//Arrays.fill(currPositions,0);
int i=0;
int prefPointer,pref;
while(i<jlist.size())
{
if(settled[i]==false)
{
prefPointer=currPositions[i];
pref=jlist.get(i).getPref(prefPointer);
int c=clist.get(pref).attemptAdd(jlist.get(i));
switch(c)
{
case 0:
settled[i]=true;
i++;
break;
case -1:
currPositions[i]++;
if(currPositions[i]==jlist.get(i).getPrefSize())
{
currPositions[i]=0;
//Add this to whatever circuit available you can
for(int availableCircuit=0;availableCircuit<clist.size();++availableCircuit)
{
//if(!clist.get(availableCircuit).isFull())
{
int status=clist.get(availableCircuit).attemptAdd(jlist.get(i));
if(status==0)
{
settled[i]=true;
i++;
break;
}
else if(status==-1)
{
continue;
}
else
{
settled[i]=true;
settled[status]=false;
currPositions[status]=0;
i=status;
}
}
}
}
break;
default:
settled[i]=true;
settled[c]=false;
currPositions[c]=0;
i=c;
}
}
else
i++;
}
}*/
public static int[] matchMaker(ArrayList<Circuit> clist,ArrayList<Juggler> jlist)
{
int currPositions[]=new int[jlist.size()];
int wherePlaced[]=new int[jlist.size()];
boolean settled[]=new boolean[jlist.size()];
boolean allsettled=false;
while(!allsettled)
{
allsettled=true;
int prefPointer,pref=0;
for(int i=0;i<jlist.size();i++)
{
if(!settled[i])
{
prefPointer=currPositions[i];
boolean placed=false;
while(!placed && (prefPointer<jlist.get(i).getPrefSize()))
{
pref=jlist.get(i).getPref(prefPointer);
int c=clist.get(pref).attemptAdd(jlist.get(i));
switch(c)
{
case 0:
settled[i]=true;
wherePlaced[i]=pref;
placed=true;
break;
case -1:
currPositions[i]++;
prefPointer=currPositions[i];
break;
default:
settled[i]=true;
settled[c]=false;
placed=true;
allsettled=false;
wherePlaced[i]=pref;
break;
}
}
if(!placed)
{
for(int j=0;j<clist.size();j++)
{
int c=clist.get(j).attemptAdd(jlist.get(i));
switch(c)
{
case 0:
settled[i]=true;
placed=true;
wherePlaced[i]=pref;
break;
case -1:
break;
default:
settled[i]=true;
settled[c]=false;
placed=true;
allsettled=false;
wherePlaced[i]=pref;
break;
}
if(placed)
break;
}
}
}
}
}
return wherePlaced;
}
/**
* Load all the Circuit objects from file
* @param br BufferedReader object that has been initialized to read from required file from required spot
* @param clist ArrayList of Circuit objects
* @return Count of total number of Circuit objects in the file
* @throws IOException
*/
public static int loadCircuits(BufferedReader br, ArrayList<Circuit> clist) throws IOException
{
String line=br.readLine();
int[] skills=new int[3];
int circuitCount=0;
while(!line.equals(""))
{
line=line.substring(line.indexOf(":")+1);
skills[0]=Integer.parseInt(line.substring(0,line.indexOf(" ")));
line=line.substring(line.indexOf(":")+1);
skills[1]=Integer.parseInt(line.substring(0,line.indexOf(" ")));
line=line.substring(line.indexOf(":")+1);
skills[2]=Integer.parseInt(line.substring(0));
clist.add(new Circuit(skills[0],skills[1],skills[2],circuitCount));
line=br.readLine();
circuitCount++;
}
return circuitCount;
}
/**
* Load all the Juggler objects from file
* @param br BufferedReader object that has been initialized to read Juggler objects from required file from required spot
* @param jlist ArrayList of Juggler objects
* @return Count of total number of Juggler objects in the file
* @throws IOException
*/
public static int loadJugglers(BufferedReader br, ArrayList<Juggler> jlist) throws IOException
{
String line=br.readLine();
int[] skills=new int[3];
String[] prefs;
int jugglerCount=0;
while(line!=null)
{
line=line.substring(line.indexOf(":")+1);
skills[0]=Integer.parseInt(line.substring(0,line.indexOf(" ")));
line=line.substring(line.indexOf(":")+1);
skills[1]=Integer.parseInt(line.substring(0,line.indexOf(" ")));
line=line.substring(line.indexOf(":")+1);
skills[2]=Integer.parseInt(line.substring(0,line.indexOf(" ")));
line=line.substring(line.indexOf(" ")+1);
prefs=line.split(",");
int[] prefInt=new int[prefs.length];
for(int i=0;i<prefs.length;i++)
{
prefInt[i]=Integer.parseInt(prefs[i].substring(1));
}
jlist.add((new Juggler(skills[0],skills[1],skills[2],jugglerCount,prefs.length)).setPref(prefInt));
jugglerCount++;
line=br.readLine();
}
return jugglerCount;
}
}