akhmetovlz@52: package ru.alphatech.icfpc.game; akhmetovlz@51: akhmetovlz@51: import java.util.ArrayList; rushan@87: import java.util.Collections; rushan@87: import java.util.List; akhmetovlz@51: akhmetovlz@51: import ru.alphatech.icfpc.cards.Dbl; akhmetovlz@51: import ru.alphatech.icfpc.cards.ICard; akhmetovlz@115: import ru.alphatech.icfpc.cards.K; akhmetovlz@51: import ru.alphatech.icfpc.cards.Put; akhmetovlz@115: import ru.alphatech.icfpc.cards.S; akhmetovlz@51: import ru.alphatech.icfpc.cards.Succ; akhmetovlz@51: import ru.alphatech.icfpc.cards.Zero; akhmetovlz@52: import ru.alphatech.icfpc.game.Batch.OperationType; akhmetovlz@51: akhmetovlz@51: /** akhmetovlz@51: * akhmetovlz@51: * TODO akhmetovlz@51: * akhmetovlz@51: * @author akhmetovlz akhmetovlz@51: * akhmetovlz@51: */ akhmetovlz@51: public abstract class AbstractPolicy { akhmetovlz@51: akhmetovlz@51: protected ArrayList batches = new ArrayList(); akhmetovlz@51: akhmetovlz@51: public AbstractPolicy(){ akhmetovlz@51: } akhmetovlz@51: akhmetovlz@51: public void setRegister(int registerIndex, int num ){ akhmetovlz@51: if( registerIndex < 4) { akhmetovlz@51: Slot registerSlot = Run.getRegisterByIndex(registerIndex); akhmetovlz@51: batches.addAll( numberIntoSlotField( registerSlot, num ) ); akhmetovlz@51: } akhmetovlz@51: } akhmetovlz@51: akhmetovlz@51: protected ArrayList numberIntoSlotField(Slot targetSlot, int number) akhmetovlz@51: { akhmetovlz@51: ArrayList retArray = new ArrayList(); akhmetovlz@51: akhmetovlz@51: IValue targetValue = targetSlot.getValue(); akhmetovlz@51: akhmetovlz@51: if(targetValue instanceof ICard){ akhmetovlz@101: retArray.addAll(smartAlg(0, number, targetSlot)); akhmetovlz@51: }else{ akhmetovlz@101: Value numTargetValue = (Value) targetValue; akhmetovlz@101: if( numTargetValue.getValue() == number ){ akhmetovlz@101: return retArray; akhmetovlz@101: }else akhmetovlz@101: if( numTargetValue.getValue() > number ){ akhmetovlz@101: retArray.addAll( smartAlg(0, number, targetSlot) ); akhmetovlz@101: }else{ akhmetovlz@101: retArray.addAll( smartAlg(numTargetValue.getValue(), number, targetSlot) ); akhmetovlz@101: } akhmetovlz@51: } akhmetovlz@51: akhmetovlz@51: return retArray; akhmetovlz@51: } akhmetovlz@51: akhmetovlz@51: akhmetovlz@51: protected ArrayList stupidAlg(int begin, int end, Slot targetSlot){ akhmetovlz@51: ArrayList retArray = new ArrayList(); akhmetovlz@51: while( begin*2 < end){ akhmetovlz@51: retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot)); akhmetovlz@51: begin *= 2; akhmetovlz@51: } akhmetovlz@51: while( begin != end){ akhmetovlz@51: retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); akhmetovlz@51: begin ++; akhmetovlz@51: } akhmetovlz@51: return retArray; akhmetovlz@51: } akhmetovlz@51: rushan@87: protected static List smartAlg(int begin, int end, Slot targetSlot){ akhmetovlz@51: ArrayList retArray = new ArrayList(); akhmetovlz@101: akhmetovlz@101: if(begin == 0){ akhmetovlz@101: retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); akhmetovlz@101: retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); akhmetovlz@101: retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); akhmetovlz@101: } rushan@87: rushan@87: List list1 = buildNumberOperations(0, end, targetSlot); rushan@87: List list2 = buildNumberOperations(begin, end, targetSlot); rushan@87: if (list1.size() <= list2.size()) rushan@87: { rushan@87: Collections.reverse(list1); rushan@87: retArray.addAll(list1); rushan@87: } rushan@87: else rushan@87: { rushan@87: Collections.reverse(list2); rushan@87: retArray.addAll(list2); akhmetovlz@51: } akhmetovlz@51: return retArray; akhmetovlz@51: } akhmetovlz@51: rushan@87: protected static List buildNumberOperations(int begin, int end, Slot targetSlot){ rushan@87: List list = new ArrayList(); rushan@87: akhmetovlz@62: while(end >= 2){ akhmetovlz@62: int d = end % 2; rushan@87: if (begin > 0 && 2*begin > end) rushan@87: { rushan@87: for (int i = 0; i < end - begin; i++) rushan@87: { rushan@87: list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); rushan@87: } rushan@87: break; rushan@87: } akhmetovlz@62: if( d == 0 ) { rushan@87: list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); rushan@87: end = end / 2; akhmetovlz@62: } else { rushan@87: list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); rushan@87: list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); rushan@87: end = (end-1) / 2; akhmetovlz@62: } akhmetovlz@62: } rushan@87: return list; akhmetovlz@62: } akhmetovlz@62: akhmetovlz@115: /** akhmetovlz@115: * Корректировка позиций регистров akhmetovlz@115: * В самом лучшем случаи регистры это слоты 0 1 2 3 akhmetovlz@115: * Но могут быть изменены, этот метод корректирует ссылки на регистры в полтиках akhmetovlz@115: * @param registerIndex - индекс регитсра (не слота!) akhmetovlz@115: * @param processingSlot - слот где происходит рассчёт akhmetovlz@115: * @return akhmetovlz@115: */ akhmetovlz@115: protected ArrayList correctRegister(int registerIndex, Slot processingSlot){ akhmetovlz@115: ArrayList retArray = new ArrayList(); akhmetovlz@115: for(int i=0; i getBatches(); rushan@87: rushan@87: public static void main(String[] args) rushan@87: { rushan@89: Slot slot = new Slot(1); akhmetovlz@101: List list = smartAlg(0, 8, slot); rushan@87: System.out.println(list.toArray().toString()); rushan@87: } akhmetovlz@51: }