Search in sources :

Example 1 with FullLimitedMino

use of _implements.parity_based_pack.step2.FullLimitedMino in project solution-finder by knewjade.

the class CrossBuilder method createList.

private void createList(int depth) {
    for (FullLimitedMino mino : sets.get(depth)) {
        if (isSame[depth] && 0 <= fullLimitedMinos[depth - 1].compareTo(mino))
            continue;
        int[][] blockCountEachLines = mino.getBlockCountEachLines();
        int[] parity = mino.getParity();
        lineCounterField.decrease(blockCountEachLines);
        lineCounterField.decrease(parity);
        if (lineCounterField.isValid()) {
            fullLimitedMinos[depth] = mino;
            if (lastIndex == depth) {
                recordResult();
            } else {
                createList(depth + 1);
            }
        }
        lineCounterField.increase(blockCountEachLines);
        lineCounterField.increase(parity);
    }
}
Also used : FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino)

Example 2 with FullLimitedMino

use of _implements.parity_based_pack.step2.FullLimitedMino 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 FullLimitedMino

use of _implements.parity_based_pack.step2.FullLimitedMino 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 4 with FullLimitedMino

use of _implements.parity_based_pack.step2.FullLimitedMino in project solution-finder by knewjade.

the class CrossBuilder method recordResult.

private void recordResult() {
    boolean isReuse = true;
    for (int index = 0; index < prev.length; index++) {
        if (!isReuse || fullLimitedMinos[index] != prev[index]) {
            isReuse = false;
            List<XField> xFields = cache.get(index);
            List<XField> nextXFields = new ArrayList<>();
            for (XField xField : xFields) {
                Field field = xField.getField();
                XHistory xHistory = xField.getxHistory();
                FullLimitedMino current = fullLimitedMinos[index];
                MinoMask minoMask = current.getMinoMask();
                int[] xs = current.getXs();
                for (int x : xs) {
                    Field mask = minoMask.getMinoMask(x);
                    if (field.canMerge(mask)) {
                        Field newField = field.freeze(maxClearLine);
                        newField.merge(mask);
                        if (perfectValidator.validate(newField, maxClearLine)) {
                            XHistory newHistory = xHistory.recordAndReturnNew(x);
                            nextXFields.add(new XField(newField, newHistory));
                        }
                    }
                }
            }
            if (index < prev.length - 1) {
                cache.set(index + 1, nextXFields);
            } else {
                for (XField nextXField : nextXFields) {
                    int[] history = nextXField.getxHistory().getHistory();
                    assert fullLimitedMinos.length == history.length;
                    List<MinoOperationWithKey> result = new ArrayList<>();
                    for (int i = 0, length = history.length; i < length; i++) {
                        FullLimitedMino limitedMino = fullLimitedMinos[i];
                        int x = history[i];
                        Mino mino = limitedMino.getMino();
                        long deleteKey = limitedMino.getDeleteKey();
                        long usingKey = limitedMino.getUsingKey();
                        int lowerY = limitedMino.getLowerY();
                        MinoOperationWithKey withKey = new FullOperationWithKey(mino, x, deleteKey, usingKey, lowerY);
                        result.add(withKey);
                    }
                    results.add(result);
                }
            }
            prev[index] = fullLimitedMinos[index];
        }
    }
}
Also used : FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) ArrayList(java.util.ArrayList) FullOperationWithKey(common.datastore.FullOperationWithKey) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) MinoMask(searcher.pack.separable_mino.mask.MinoMask) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) Mino(core.mino.Mino)

Example 5 with FullLimitedMino

use of _implements.parity_based_pack.step2.FullLimitedMino in project solution-finder by knewjade.

the class Search method search.

private void search(Field field, int depth) {
    if (depth == sets.getDepth()) {
        List<OperationWithKey> result = new ArrayList<>();
        for (int index = 0, length = fullLimitedMinos.length; index < length; index++) {
            FullLimitedMino limitedMino = fullLimitedMinos[index];
            int x = xs[index];
            Mino mino = limitedMino.getMino();
            long deleteKey = limitedMino.getDeleteKey();
            long usingKey = limitedMino.getUsingKey();
            int lowerY = limitedMino.getLowerY();
            OperationWithKey withKey = new FullOperationWithKey(mino, x, deleteKey, usingKey, lowerY);
            result.add(withKey);
        }
        results.add(result);
    } else {
        FullLimitedMino limitedMino = sets.get(depth);
        fullLimitedMinos[depth] = limitedMino;
        MinoMask minoMask = limitedMino.getMinoMask();
        for (int x : limitedMino.getXs()) {
            xs[depth] = x;
            Field mask = minoMask.getMinoMask(x);
            if (field.canMerge(mask)) {
                field.merge(mask);
                if (perfectValidator.validate(field, maxClearLine))
                    search(field, depth + 1);
                field.reduce(mask);
            }
        }
    }
}
Also used : FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) Field(core.field.Field) OperationWithKey(common.datastore.OperationWithKey) FullOperationWithKey(common.datastore.FullOperationWithKey) MinoMask(searcher.pack.separable_mino.mask.MinoMask) ArrayList(java.util.ArrayList) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) Mino(core.mino.Mino) FullOperationWithKey(common.datastore.FullOperationWithKey)

Aggregations

FullLimitedMino (_implements.parity_based_pack.step2.FullLimitedMino)5 Field (core.field.Field)4 Mino (core.mino.Mino)4 ColumnParityLimitation (_implements.parity_based_pack.step1.ColumnParityLimitation)2 DeltaLimitedMino (_implements.parity_based_pack.step1.DeltaLimitedMino)2 EstimateBuilder (_implements.parity_based_pack.step1.EstimateBuilder)2 PositionLimitParser (_implements.parity_based_pack.step2.PositionLimitParser)2 CrossBuilder (_implements.parity_based_pack.step3.CrossBuilder)2 BuildUp (common.buildup.BuildUp)2 OperationWithKeyComparator (common.comparator.OperationWithKeyComparator)2 common.datastore (common.datastore)2 FullOperationWithKey (common.datastore.FullOperationWithKey)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 ColorType (common.tetfu.common.ColorType)2 ColoredField (common.tetfu.field.ColoredField)2 ColoredFieldFactory (common.tetfu.field.ColoredFieldFactory)2