Search in sources :

Example 1 with CrossBuilder

use of _implements.parity_based_pack.step3.CrossBuilder in project solution-finder by knewjade.

the class Main3 method main.

public static void main(String[] args) {
    Field field = FieldFactory.createField("" + "X_______XX" + "X_______XX" + "X_______XX" + "X_______XX");
    int maxClearLine = 4;
    Field verifyField = field.freeze(maxClearLine);
    System.out.println(FieldView.toString(field, maxClearLine));
    Stopwatch stopwatch = Stopwatch.createStoppedStopwatch();
    stopwatch.start();
    Set<List<Piece>> sets = new HashSet<>();
    List<Piece> allPieces = Piece.valueList();
    List<Piece> pieces = new ArrayList<>(allPieces);
    int popCount = (maxClearLine * 10 - field.getNumOfAllBlocks()) / 4;
    CombinationIterable<Piece> combinationIterable = new CombinationIterable<>(pieces, popCount);
    for (List<Piece> pieceList : combinationIterable) {
        pieceList.sort(Comparator.comparingInt(allPieces::indexOf));
        sets.add(pieceList);
    }
    TreeSet<Obj> allObjSet = new TreeSet<>();
    int counter = 0;
    for (List<Piece> usedPieces : sets) {
        counter++;
        System.out.println(usedPieces);
        System.out.println(counter + " / " + sets.size());
        List<List<MinoOperationWithKey>> operationsWithKey = search(usedPieces, field, maxClearLine, verifyField);
        List<Obj> objs = operationsWithKey.stream().map(operationWithKeys -> {
            boolean isDeleted = false;
            BlockField blockField = new BlockField(maxClearLine);
            for (MinoOperationWithKey key : operationWithKeys) {
                Field test = FieldFactory.createField(maxClearLine);
                Mino mino = key.getMino();
                test.put(mino, key.getX(), key.getY());
                test.insertWhiteLineWithKey(key.getNeedDeletedKey());
                blockField.merge(test, mino.getPiece());
                if (key.getNeedDeletedKey() != 0L)
                    isDeleted = true;
            }
            return new Obj(usedPieces, blockField, isDeleted, operationWithKeys);
        }).collect(Collectors.toList());
        System.out.println(objs.size());
        objs.stream().map(obj -> OperationWithKeyInterpreter.parseToString(obj.operations)).sorted().sequential().forEach(System.out::println);
        allObjSet.addAll(objs);
    }
    System.out.println(allObjSet.size());
    System.exit(0);
    ArrayList<Obj> allObjs = new ArrayList<>(allObjSet);
    allObjs.sort(Main3::blockListComparator);
    // System.out.println(operationsWithKey.size());
    ColorConverter colorConverter = new ColorConverter();
    System.out.println("<h3>各ミノ最大1つずつ & ライン消去なし</h3>");
    Predicate<Obj> objPredicate = obj -> !obj.isDouble && !obj.isDeleted;
    List<TetfuElement> oneNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate);
    viewTetfu(oneNoDelete);
    System.out.println("<h3>同一ミノを2つ利用 & ライン消去なし</h3>");
    Predicate<Obj> objPredicate1 = obj -> obj.isDouble && !obj.isDeleted;
    List<TetfuElement> doubleNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate1);
    viewTetfu(doubleNoDelete);
    System.out.println("<h3>各ミノ最大1つずつ & ライン消去あり</h3>");
    Predicate<Obj> objPredicate2 = obj -> !obj.isDouble && obj.isDeleted;
    List<TetfuElement> oneDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate2);
    viewTetfu(oneDeleted);
    System.out.println("<h3>同一ミノを2つ利用 & ライン消去あり</h3>");
    Predicate<Obj> objPredicate3 = obj -> obj.isDouble && obj.isDeleted;
    List<TetfuElement> doubleDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate3);
    viewTetfu(doubleDeleted);
    // List<List<FullLimitedMino>> lists = Arrays.asList(
    // singletonList(create(minoFactory, Piece.I, Rotate.Left, PositionLimit.OddX, 0L, 0, 3)),
    // singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 2, 3)),
    // singletonList(create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.Z, Rotate.Spawn, PositionLimit.EvenX, 0L, 2, 3)),
    // singletonList(create(minoFactory, Piece.S, Rotate.Left, PositionLimit.OddX, 1024L, 0, 3)),
    // singletonList(create(minoFactory, Piece.Z, Rotate.Left, PositionLimit.EvenX, 1024L, 0, 3)),
    // singletonList(create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.EvenX, 0L, 2, 3))
    // );
    // CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField(maxClearLine), maxClearLine);
    // List<List<OperationWithKey>> lists1 = crossBuilder.create();
    // System.out.println(lists1.size());
    // List<List<OperationWithKey>> search = new Search(FieldFactory.createField(4), Arrays.asList(
    // create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 2, 3),
    // create(minoFactory, Piece.I, Rotate.Right, PositionLimit.NoLimit, 0L, 0, 3),
    // create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.NoLimit, 0L, 2, 3),
    // create(minoFactory, Piece.Z, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3),
    // create(minoFactory, Piece.S, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3)
    // ), maxClearLine).search();
    // System.out.println("--");
    // for (List<OperationWithKey> operationWithKeys : search) {
    // System.out.println(operationWithKeys);
    // }
    // MinoFactory minoFactory = new MinoFactory();
    // List<List<FullLimitedMino>> lists = Arrays.asList(
    // singletonList(create(minoFactory, Piece.J, Rotate.Right, PositionLimit.OddX, 0L, 0, 2)),
    // singletonList(create(minoFactory, Piece.L, Rotate.Left, PositionLimit.EvenX, 1048576, 0, 4))
    // );
    // CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField("" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX"
    // ), maxClearLine);
    // List<List<OperationWithKey>> lists1 = crossBuilder.create();
    // for (List<OperationWithKey> operationWithKeys : lists1) {
    // System.out.println(operationWithKeys);
    // }
    // System.out.println(lists1.size());
    stopwatch.stop();
    System.out.println(stopwatch.toMessage(TimeUnit.MILLISECONDS));
}
Also used : java.util(java.util) Stopwatch(lib.Stopwatch) ColorType(common.tetfu.common.ColorType) OperationWithKeyComparator(common.comparator.OperationWithKeyComparator) common.datastore(common.datastore) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Tetfu(common.tetfu.Tetfu) FieldView(core.field.FieldView) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) CombinationIterable(common.iterable.CombinationIterable) FieldFactory(core.field.FieldFactory) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) Predicate(java.util.function.Predicate) BuildUp(common.buildup.BuildUp) TetfuElement(common.tetfu.TetfuElement) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) TimeUnit(java.util.concurrent.TimeUnit) Field(core.field.Field) Stream(java.util.stream.Stream) OperationWithKeyInterpreter(common.parser.OperationWithKeyInterpreter) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) Stopwatch(lib.Stopwatch) Field(core.field.Field) ColoredField(common.tetfu.field.ColoredField) CombinationIterable(common.iterable.CombinationIterable) Piece(core.mino.Piece) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) Mino(core.mino.Mino) ColorConverter(common.tetfu.common.ColorConverter) TetfuElement(common.tetfu.TetfuElement)

Example 2 with CrossBuilder

use of _implements.parity_based_pack.step3.CrossBuilder in project solution-finder by knewjade.

the class Main3 method search.

public static List<List<MinoOperationWithKey>> search(List<Piece> usedPieces, Field field, int maxClearLine, Field verifyField) {
    MinoFactory minoFactory = new MinoFactory();
    PositionLimitParser positionLimitParser = new PositionLimitParser(minoFactory, maxClearLine);
    LockedReachableThreadLocal threadLocal = new LockedReachableThreadLocal(maxClearLine);
    ParityField parityField = new ParityField(field);
    PieceCounter pieceCounter = new PieceCounter(usedPieces);
    ColumnParityLimitation limitation = new ColumnParityLimitation(pieceCounter, parityField, maxClearLine);
    return limitation.enumerate().parallelStream().map(EstimateBuilder::create).flatMap(Collection::stream).flatMap((List<DeltaLimitedMino> deltaLimitedMinos) -> {
        // 変換 DeltaLimitedMinos to FullLimitedMino
        List<List<FullLimitedMino>> collect = deltaLimitedMinos.stream().map(positionLimitParser::parse).collect(Collectors.toList());
        // 候補数が小さい順  // 同種のブロックを固めるため
        List<Piece> priority = collect.stream().sorted(Comparator.comparingInt(List::size)).map(fullLimitedMinos -> fullLimitedMinos.get(0).getMino().getPiece()).collect(Collectors.toList());
        // ソートする
        collect.sort((o1, o2) -> {
            int compare = Integer.compare(priority.indexOf(o1.get(0).getMino().getPiece()), priority.indexOf(o2.get(0).getMino().getPiece()));
            if (compare != 0)
                return compare;
            return -Integer.compare(o1.size(), o2.size());
        });
        return Stream.of(collect);
    }).limit(Long.MAX_VALUE).flatMap(sets -> new CrossBuilder(sets, field, maxClearLine).create().stream()).filter(operationWithKeys -> BuildUp.existsValidBuildPattern(verifyField, operationWithKeys, maxClearLine, threadLocal.get())).collect(Collectors.toList());
}
Also used : PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) java.util(java.util) Stopwatch(lib.Stopwatch) ColorType(common.tetfu.common.ColorType) OperationWithKeyComparator(common.comparator.OperationWithKeyComparator) common.datastore(common.datastore) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Tetfu(common.tetfu.Tetfu) FieldView(core.field.FieldView) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) CombinationIterable(common.iterable.CombinationIterable) FieldFactory(core.field.FieldFactory) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) Predicate(java.util.function.Predicate) BuildUp(common.buildup.BuildUp) TetfuElement(common.tetfu.TetfuElement) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) TimeUnit(java.util.concurrent.TimeUnit) Field(core.field.Field) Stream(java.util.stream.Stream) OperationWithKeyInterpreter(common.parser.OperationWithKeyInterpreter) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) MinoFactory(core.mino.MinoFactory)

Example 3 with CrossBuilder

use of _implements.parity_based_pack.step3.CrossBuilder in project solution-finder by knewjade.

the class ParityBasedPackSearcher method search.

public Stream<List<MinoOperationWithKey>> search(List<Piece> usingPieces) {
    // 準備
    MinoFactory minoFactory = new MinoFactory();
    PositionLimitParser positionLimitParser = new PositionLimitParser(minoFactory, maxClearLine);
    LockedReachableThreadLocal threadLocal = new LockedReachableThreadLocal(maxClearLine);
    ParityField parityField = new ParityField(field);
    PieceCounter pieceCounter = new PieceCounter(usingPieces);
    ColumnParityLimitation limitation = new ColumnParityLimitation(pieceCounter, parityField, maxClearLine);
    return limitation.enumerate().parallelStream().map(EstimateBuilder::create).flatMap(Collection::stream).flatMap(deltaLimitedMinos -> parseToSortedFullLimitedMinoStream(positionLimitParser, deltaLimitedMinos)).limit(// parallelでの並列数をリセットする(同時実行数を増やす)
    Long.MAX_VALUE).flatMap(sets -> new CrossBuilder(sets, field, maxClearLine).create().stream()).filter(operationWithKeys -> BuildUp.existsValidBuildPattern(verifyField, operationWithKeys, maxClearLine, threadLocal.get()));
}
Also used : PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) BuildUp(common.buildup.BuildUp) Collection(java.util.Collection) MinoOperationWithKey(common.datastore.MinoOperationWithKey) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Collectors(java.util.stream.Collectors) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) PieceCounter(common.datastore.PieceCounter) List(java.util.List) Field(core.field.Field) Stream(java.util.stream.Stream) MinoFactory(core.mino.MinoFactory) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Comparator(java.util.Comparator) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) PieceCounter(common.datastore.PieceCounter) Collection(java.util.Collection) MinoFactory(core.mino.MinoFactory) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation)

Aggregations

ColumnParityLimitation (_implements.parity_based_pack.step1.ColumnParityLimitation)3 DeltaLimitedMino (_implements.parity_based_pack.step1.DeltaLimitedMino)3 EstimateBuilder (_implements.parity_based_pack.step1.EstimateBuilder)3 FullLimitedMino (_implements.parity_based_pack.step2.FullLimitedMino)3 PositionLimitParser (_implements.parity_based_pack.step2.PositionLimitParser)3 CrossBuilder (_implements.parity_based_pack.step3.CrossBuilder)3 BuildUp (common.buildup.BuildUp)3 LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)3 Field (core.field.Field)3 MinoFactory (core.mino.MinoFactory)3 Piece (core.mino.Piece)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 OperationWithKeyComparator (common.comparator.OperationWithKeyComparator)2 common.datastore (common.datastore)2 CombinationIterable (common.iterable.CombinationIterable)2 OperationWithKeyInterpreter (common.parser.OperationWithKeyInterpreter)2 Tetfu (common.tetfu.Tetfu)2 TetfuElement (common.tetfu.TetfuElement)2 ColorConverter (common.tetfu.common.ColorConverter)2