|
akhmetovlz@52
|
1 |
package ru.alphatech.icfpc.game;
|
|
akhmetovlz@51
|
2 |
|
|
akhmetovlz@51
|
3 |
import java.util.ArrayList;
|
|
rushan@87
|
4 |
import java.util.Collections;
|
|
rushan@87
|
5 |
import java.util.List;
|
|
akhmetovlz@51
|
6 |
|
|
akhmetovlz@51
|
7 |
import ru.alphatech.icfpc.cards.Dbl;
|
|
akhmetovlz@51
|
8 |
import ru.alphatech.icfpc.cards.ICard;
|
|
akhmetovlz@115
|
9 |
import ru.alphatech.icfpc.cards.K;
|
|
akhmetovlz@51
|
10 |
import ru.alphatech.icfpc.cards.Put;
|
|
akhmetovlz@115
|
11 |
import ru.alphatech.icfpc.cards.S;
|
|
akhmetovlz@51
|
12 |
import ru.alphatech.icfpc.cards.Succ;
|
|
akhmetovlz@51
|
13 |
import ru.alphatech.icfpc.cards.Zero;
|
|
akhmetovlz@52
|
14 |
import ru.alphatech.icfpc.game.Batch.OperationType;
|
|
akhmetovlz@51
|
15 |
|
|
akhmetovlz@51
|
16 |
/**
|
|
akhmetovlz@51
|
17 |
*
|
|
akhmetovlz@51
|
18 |
* TODO
|
|
akhmetovlz@51
|
19 |
*
|
|
akhmetovlz@51
|
20 |
* @author akhmetovlz
|
|
akhmetovlz@51
|
21 |
*
|
|
akhmetovlz@51
|
22 |
*/
|
|
akhmetovlz@51
|
23 |
public abstract class AbstractPolicy {
|
|
akhmetovlz@51
|
24 |
|
|
akhmetovlz@51
|
25 |
protected ArrayList<Batch> batches = new ArrayList<Batch>();
|
|
akhmetovlz@51
|
26 |
|
|
akhmetovlz@51
|
27 |
public AbstractPolicy(){
|
|
akhmetovlz@51
|
28 |
}
|
|
akhmetovlz@51
|
29 |
|
|
akhmetovlz@51
|
30 |
public void setRegister(int registerIndex, int num ){
|
|
akhmetovlz@51
|
31 |
if( registerIndex < 4) {
|
|
akhmetovlz@51
|
32 |
Slot registerSlot = Run.getRegisterByIndex(registerIndex);
|
|
akhmetovlz@51
|
33 |
batches.addAll( numberIntoSlotField( registerSlot, num ) );
|
|
akhmetovlz@51
|
34 |
}
|
|
akhmetovlz@51
|
35 |
}
|
|
akhmetovlz@51
|
36 |
|
|
akhmetovlz@51
|
37 |
protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
|
|
akhmetovlz@51
|
38 |
{
|
|
akhmetovlz@51
|
39 |
ArrayList<Batch> retArray = new ArrayList<Batch>();
|
|
akhmetovlz@51
|
40 |
|
|
akhmetovlz@120
|
41 |
retArray.addAll(smartAlg(0, number, targetSlot));
|
|
akhmetovlz@51
|
42 |
|
|
akhmetovlz@51
|
43 |
return retArray;
|
|
akhmetovlz@51
|
44 |
}
|
|
akhmetovlz@51
|
45 |
|
|
akhmetovlz@51
|
46 |
|
|
akhmetovlz@51
|
47 |
protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
|
|
akhmetovlz@51
|
48 |
ArrayList<Batch> retArray = new ArrayList<Batch>();
|
|
akhmetovlz@51
|
49 |
while( begin*2 < end){
|
|
akhmetovlz@51
|
50 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
|
|
akhmetovlz@51
|
51 |
begin *= 2;
|
|
akhmetovlz@51
|
52 |
}
|
|
akhmetovlz@51
|
53 |
while( begin != end){
|
|
akhmetovlz@51
|
54 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
|
|
akhmetovlz@51
|
55 |
begin ++;
|
|
akhmetovlz@51
|
56 |
}
|
|
akhmetovlz@51
|
57 |
return retArray;
|
|
akhmetovlz@51
|
58 |
}
|
|
akhmetovlz@51
|
59 |
|
|
rushan@87
|
60 |
protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
|
|
akhmetovlz@51
|
61 |
ArrayList<Batch> retArray = new ArrayList<Batch>();
|
|
akhmetovlz@101
|
62 |
|
|
akhmetovlz@101
|
63 |
if(begin == 0){
|
|
akhmetovlz@101
|
64 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
|
|
akhmetovlz@101
|
65 |
retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
|
|
akhmetovlz@101
|
66 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
|
|
akhmetovlz@101
|
67 |
}
|
|
rushan@87
|
68 |
|
|
rushan@87
|
69 |
List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
|
|
rushan@87
|
70 |
List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
|
|
rushan@87
|
71 |
if (list1.size() <= list2.size())
|
|
rushan@87
|
72 |
{
|
|
rushan@87
|
73 |
Collections.reverse(list1);
|
|
rushan@87
|
74 |
retArray.addAll(list1);
|
|
rushan@87
|
75 |
}
|
|
rushan@87
|
76 |
else
|
|
rushan@87
|
77 |
{
|
|
rushan@87
|
78 |
Collections.reverse(list2);
|
|
rushan@87
|
79 |
retArray.addAll(list2);
|
|
akhmetovlz@51
|
80 |
}
|
|
akhmetovlz@51
|
81 |
return retArray;
|
|
akhmetovlz@51
|
82 |
}
|
|
akhmetovlz@51
|
83 |
|
|
rushan@87
|
84 |
protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
|
|
rushan@87
|
85 |
List<Batch> list = new ArrayList<Batch>();
|
|
rushan@87
|
86 |
|
|
akhmetovlz@62
|
87 |
while(end >= 2){
|
|
akhmetovlz@62
|
88 |
int d = end % 2;
|
|
rushan@87
|
89 |
if (begin > 0 && 2*begin > end)
|
|
rushan@87
|
90 |
{
|
|
rushan@87
|
91 |
for (int i = 0; i < end - begin; i++)
|
|
rushan@87
|
92 |
{
|
|
rushan@87
|
93 |
list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
|
|
rushan@87
|
94 |
}
|
|
rushan@87
|
95 |
break;
|
|
rushan@87
|
96 |
}
|
|
akhmetovlz@62
|
97 |
if( d == 0 ) {
|
|
rushan@87
|
98 |
list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
|
|
rushan@87
|
99 |
end = end / 2;
|
|
akhmetovlz@62
|
100 |
} else {
|
|
rushan@87
|
101 |
list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
|
|
rushan@87
|
102 |
list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
|
|
rushan@87
|
103 |
end = (end-1) / 2;
|
|
akhmetovlz@62
|
104 |
}
|
|
akhmetovlz@62
|
105 |
}
|
|
rushan@87
|
106 |
return list;
|
|
akhmetovlz@62
|
107 |
}
|
|
akhmetovlz@62
|
108 |
|
|
akhmetovlz@115
|
109 |
/**
|
|
akhmetovlz@115
|
110 |
* Корректировка позиций регистров
|
|
akhmetovlz@115
|
111 |
* В самом лучшем случаи регистры это слоты 0 1 2 3
|
|
akhmetovlz@115
|
112 |
* Но могут быть изменены, этот метод корректирует ссылки на регистры в полтиках
|
|
akhmetovlz@115
|
113 |
* @param registerIndex - индекс регитсра (не слота!)
|
|
akhmetovlz@115
|
114 |
* @param processingSlot - слот где происходит рассчёт
|
|
akhmetovlz@115
|
115 |
* @return
|
|
akhmetovlz@115
|
116 |
*/
|
|
akhmetovlz@115
|
117 |
protected ArrayList<Batch> correctRegister(int registerIndex, Slot processingSlot){
|
|
akhmetovlz@115
|
118 |
ArrayList<Batch> retArray = new ArrayList<Batch>();
|
|
akhmetovlz@118
|
119 |
for(int i=0; i<Run.getRegisterSlotNum(registerIndex); ++i)
|
|
akhmetovlz@115
|
120 |
{
|
|
akhmetovlz@115
|
121 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
|
|
akhmetovlz@115
|
122 |
retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
|
|
akhmetovlz@115
|
123 |
|
|
akhmetovlz@115
|
124 |
retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
|
|
akhmetovlz@115
|
125 |
}
|
|
akhmetovlz@115
|
126 |
return retArray;
|
|
akhmetovlz@115
|
127 |
}
|
|
akhmetovlz@115
|
128 |
|
|
akhmetovlz@51
|
129 |
public abstract ArrayList<Batch> getBatches();
|
|
rushan@87
|
130 |
|
|
rushan@87
|
131 |
public static void main(String[] args)
|
|
rushan@87
|
132 |
{
|
|
rushan@89
|
133 |
Slot slot = new Slot(1);
|
|
akhmetovlz@101
|
134 |
List<Batch> list = smartAlg(0, 8, slot);
|
|
rushan@87
|
135 |
System.out.println(list.toArray().toString());
|
|
rushan@87
|
136 |
}
|
|
akhmetovlz@51
|
137 |
}
|