Search in sources :

Example 1 with CombinationIterable

use of common.iterable.CombinationIterable in project solution-finder by knewjade.

the class AbstractAllOperationFactory method generatePiecesEachMino.

private ArrayList<T> generatePiecesEachMino(Mino mino, ArrayList<Integer> lineIndexes, int minoHeight) {
    ArrayList<T> pieces = new ArrayList<>();
    // ブロックが置かれる行を選択する
    CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
    for (List<Integer> indexes : combinationIterable) {
        // ソートする
        indexes.sort(Integer::compare);
        // 一番下の行と一番上の行を取得
        int lowerY = indexes.get(0);
        int upperY = indexes.get(indexes.size() - 1);
        // ミノに挟まれる全ての行を含むdeleteKey
        long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
        long usingKey = 0L;
        assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
        for (Integer index : indexes) {
            long bitKey = KeyOperators.getDeleteBitKey(index);
            // ブロックのある行のフラグを取り消す
            deleteKey &= ~bitKey;
            // ブロックのある行にフラグをたてる
            usingKey |= bitKey;
        }
        assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
        if ((deleteKeyMask & deleteKey) == deleteKey) {
            for (int x = -mino.getMinX(); x < fieldWidth - mino.getMinX(); x++) {
                FullOperationWithKey operationWithKey = new FullOperationWithKey(mino, x, lowerY - mino.getMinY(), deleteKey, usingKey);
                pieces.add(parseOperation(operationWithKey, upperY, fieldHeight));
            }
        }
    }
    return pieces;
}
Also used : CombinationIterable(common.iterable.CombinationIterable) ArrayList(java.util.ArrayList) FullOperationWithKey(common.datastore.FullOperationWithKey)

Example 2 with CombinationIterable

use of common.iterable.CombinationIterable 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 3 with CombinationIterable

use of common.iterable.CombinationIterable in project solution-finder by knewjade.

the class DeleteKeyParser method initializeMaps.

private EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> initializeMaps(MinoFactory minoFactory, int maxClearLine) {
    EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> maps = new EnumMap<>(Piece.class);
    for (Piece piece : Piece.values()) {
        EnumMap<Rotate, List<DeleteKey>> rotateMaps = maps.computeIfAbsent(piece, blk -> new EnumMap<>(Rotate.class));
        for (Rotate rotate : Rotate.values()) {
            Mino mino = minoFactory.create(piece, rotate);
            // ミノの高さを計算
            int minoHeight = mino.getMaxY() - mino.getMinY() + 1;
            // 行候補をリストにする
            ArrayList<Integer> lineIndexes = new ArrayList<>();
            for (int index = 0; index < maxClearLine; index++) lineIndexes.add(index);
            // ブロックが置かれる行を選択する
            CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
            // リストアップ
            ArrayList<DeleteKey> deleteLimitedMinos = new ArrayList<>();
            for (List<Integer> indexes : combinationIterable) {
                // ソートする
                indexes.sort(Integer::compare);
                // 一番下の行と一番上の行を取得
                int lowerY = indexes.get(0);
                int upperY = indexes.get(indexes.size() - 1);
                // ミノに挟まれる全ての行を含むdeleteKey
                long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
                long usingKey = 0L;
                assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
                for (Integer index : indexes) {
                    long bitKey = KeyOperators.getDeleteBitKey(index);
                    // ブロックのある行のフラグを取り消す
                    deleteKey &= ~bitKey;
                    // ブロックのある行のフラグをたてる
                    usingKey |= bitKey;
                }
                assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
                deleteLimitedMinos.add(DeleteKey.create(mino, deleteKey, usingKey, lowerY, upperY));
            }
            rotateMaps.put(rotate, deleteLimitedMinos);
        }
    }
    return maps;
}
Also used : Rotate(core.srs.Rotate) ArrayList(java.util.ArrayList) CombinationIterable(common.iterable.CombinationIterable) Piece(core.mino.Piece) Mino(core.mino.Mino) List(java.util.List) ArrayList(java.util.ArrayList) EnumMap(java.util.EnumMap)

Example 4 with CombinationIterable

use of common.iterable.CombinationIterable in project solution-finder by knewjade.

the class AllMinoFactory method generatePiecesEachMino.

private ArrayList<SeparableMino> generatePiecesEachMino(Mino mino, ArrayList<Integer> lineIndexes, int minoHeight) {
    ArrayList<SeparableMino> pieces = new ArrayList<>();
    // ブロックが置かれる行を選択する
    CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
    for (List<Integer> indexes : combinationIterable) {
        // ソートする
        indexes.sort(Integer::compare);
        // 一番下の行と一番上の行を取得
        int lowerY = indexes.get(0);
        int upperY = indexes.get(indexes.size() - 1);
        // ミノに挟まれる全ての行を含むdeleteKey
        long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
        long usingKey = 0L;
        assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
        for (Integer index : indexes) {
            long bitKey = KeyOperators.getDeleteBitKey(index);
            // ブロックのある行のフラグを取り消す
            deleteKey &= ~bitKey;
            // ブロックのある行にフラグをたてる
            usingKey |= bitKey;
        }
        assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
        if ((deleteKeyMask & deleteKey) == deleteKey) {
            for (int x = -mino.getMinX(); x < fieldWidth - mino.getMaxX(); x++) {
                FullOperationWithKey operationWithKey = new FullOperationWithKey(mino, x, lowerY - mino.getMinY(), deleteKey, usingKey);
                pieces.add(parseOperation(operationWithKey, upperY, fieldHeight));
            }
        }
    }
    return pieces;
}
Also used : CombinationIterable(common.iterable.CombinationIterable) ArrayList(java.util.ArrayList) FullOperationWithKey(common.datastore.FullOperationWithKey)

Example 5 with CombinationIterable

use of common.iterable.CombinationIterable in project solution-finder by knewjade.

the class AllSeparableMinoFactory method generatePiecesEachMino.

private ArrayList<SeparableMino> generatePiecesEachMino(Mino mino, ArrayList<Integer> lineIndexes, int minoHeight) {
    ArrayList<SeparableMino> pieces = new ArrayList<>();
    // ブロックが置かれる行を選択する
    CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
    for (List<Integer> indexes : combinationIterable) {
        // ソートする
        indexes.sort(Integer::compare);
        // 一番下の行と一番上の行を取得
        int lowerY = indexes.get(0);
        int upperY = indexes.get(indexes.size() - 1);
        // ミノに挟まれる全ての行を含むdeleteKey
        long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
        long usingKey = 0L;
        assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
        for (Integer index : indexes) {
            long bitKey = KeyOperators.getDeleteBitKey(index);
            // ブロックのある行のフラグを取り消す
            deleteKey &= ~bitKey;
            // ブロックのある行にフラグをたてる
            usingKey |= bitKey;
        }
        assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
        if ((deleteKeyMask & deleteKey) == deleteKey) {
            for (int x = -mino.getMinX(); x < fieldWidth - mino.getMinX(); x++) {
                FullOperationWithKey operationWithKey = new FullOperationWithKey(mino, x, lowerY - mino.getMinY(), deleteKey, usingKey);
                pieces.add(parseOperation(operationWithKey, upperY, fieldHeight));
            }
        }
    }
    return pieces;
}
Also used : CombinationIterable(common.iterable.CombinationIterable) ArrayList(java.util.ArrayList) FullOperationWithKey(common.datastore.FullOperationWithKey)

Aggregations

CombinationIterable (common.iterable.CombinationIterable)7 Piece (core.mino.Piece)4 ArrayList (java.util.ArrayList)4 FullOperationWithKey (common.datastore.FullOperationWithKey)3 LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)3 PieceCounter (common.datastore.PieceCounter)2 Mino (core.mino.Mino)2 Rotate (core.srs.Rotate)2 UsingBlockAndValidKeySolutionFilter (searcher.pack.memento.UsingBlockAndValidKeySolutionFilter)2 ColumnParityLimitation (_implements.parity_based_pack.step1.ColumnParityLimitation)1 DeltaLimitedMino (_implements.parity_based_pack.step1.DeltaLimitedMino)1 EstimateBuilder (_implements.parity_based_pack.step1.EstimateBuilder)1 FullLimitedMino (_implements.parity_based_pack.step2.FullLimitedMino)1 PositionLimitParser (_implements.parity_based_pack.step2.PositionLimitParser)1 CrossBuilder (_implements.parity_based_pack.step3.CrossBuilder)1 BuildUp (common.buildup.BuildUp)1 OperationWithKeyComparator (common.comparator.OperationWithKeyComparator)1 common.datastore (common.datastore)1 OperationWithKeyInterpreter (common.parser.OperationWithKeyInterpreter)1 Tetfu (common.tetfu.Tetfu)1