Search in sources :

Example 1 with RecursiveMinoField

use of searcher.pack.mino_field.RecursiveMinoField in project solution-finder by knewjade.

the class ConnectionsToStreamCallable method parseWhenNext.

private Stream<RecursiveMinoField> parseWhenNext(ColumnField outerColumnField, SeparableMino currentMino, RecursiveMinoFields minoFields) {
    SeparableMinos separableMinos = calculator.getSeparableMinos();
    int index = separableMinos.toIndex(currentMino);
    return minoFields.recursiveStream().filter(minoField -> minoField.getMaxIndex() <= index).map(minoField -> {
        // outerで、最終的に使用されるブロック と すでに使っているブロックが重ならないことを確認
        ColumnField lastOuterField = minoField.getOuterField();
        if (!lastOuterField.canMerge(outerColumnField))
            return null;
        OperationWithKey currentOperations = currentMino.toMinoOperationWithKey();
        long currentDeleteKey = currentOperations.getNeedDeletedKey();
        long currentUsingKey = currentOperations.getUsingKey();
        // いま置こうとしているミノと、それまでの結果に矛盾があるか確認
        boolean isContradiction = currentDeleteKey != 0L && minoField.getOperationsStream().anyMatch(operationWithKey -> {
            long deletedKey = operationWithKey.getNeedDeletedKey();
            long usingKey = operationWithKey.getUsingKey();
            return (currentUsingKey & deletedKey) != 0L && (usingKey & currentDeleteKey) != 0L;
        });
        // 矛盾があるときはスキップ
        if (isContradiction)
            return null;
        // 使用されるブロックを算出
        ColumnField usingBlock = lastOuterField.freeze(calculator.getHeight());
        usingBlock.merge(outerColumnField);
        return new RecursiveMinoField(currentMino, minoField, usingBlock, separableMinos);
    }).filter(Objects::nonNull);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields) ColumnField(core.column_field.ColumnField) Callable(java.util.concurrent.Callable) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects) List(java.util.List) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) SeparableMinos(searcher.pack.SeparableMinos) ColumnFieldConnection(searcher.pack.connections.ColumnFieldConnection) ColumnFieldConnections(searcher.pack.connections.ColumnFieldConnections) OperationWithKey(common.datastore.OperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects)

Example 2 with RecursiveMinoField

use of searcher.pack.mino_field.RecursiveMinoField in project solution-finder by knewjade.

the class ConnectionsToStreamCallable method over.

private Stream<RecursiveMinoField> over(ColumnField columnField, ColumnField outerColumnField, List<SeparableMino> minos, SeparableMinos separableMinos, RecursiveMinoField result) {
    Stream.Builder<RecursiveMinoField> builder = Stream.builder();
    builder.accept(result);
    StreamColumnFieldConnections connections = new StreamColumnFieldConnections(minos, columnField, calculator.getSizedBit());
    Stream<RecursiveMinoField> stream = connections.getConnectionStream().filter(connection -> {
        ColumnField nextOuterField = connection.getOuterField();
        SeparableMino mino = connection.getMino();
        return // 次に埋めるべき場所を埋めてある
        !needFilledField.canMerge(mino.getField()) && // 次のフィールドの制限範囲と重なっていない
        nextOuterField.canMerge(limitOuterField) && // 次のフィールドにあるブロックと重なっていない
        nextOuterField.canMerge(outerColumnField);
    }).flatMap(connection -> {
        // 使用されるブロックを算出
        ColumnField usingBlock = connection.getOuterField().freeze(calculator.getHeight());
        usingBlock.merge(outerColumnField);
        RecursiveMinoField t = new RecursiveMinoField(connection.getMino(), result, usingBlock, separableMinos);
        List<SeparableMino> allMinos = separableMinos.getMinos();
        List<SeparableMino> minos2 = allMinos.subList(separableMinos.toIndex(connection.getMino()) + 1, allMinos.size());
        return over(connection.getInnerField(), usingBlock, minos2, separableMinos, t);
    });
    return Stream.concat(Stream.of(result), stream);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields) ColumnField(core.column_field.ColumnField) Callable(java.util.concurrent.Callable) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects) List(java.util.List) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) SeparableMinos(searcher.pack.SeparableMinos) ColumnFieldConnection(searcher.pack.connections.ColumnFieldConnection) ColumnFieldConnections(searcher.pack.connections.ColumnFieldConnections) SeparableMino(searcher.pack.separable_mino.SeparableMino) ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Stream(java.util.stream.Stream) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections)

Example 3 with RecursiveMinoField

use of searcher.pack.mino_field.RecursiveMinoField in project solution-finder by knewjade.

the class ConnectionsToStreamCallable method parseWhenFilled.

private Stream<RecursiveMinoField> parseWhenFilled(ColumnField columnField, ColumnField outerColumnField, Field wallField, SeparableMino currentMino) {
    // これからブロックをおく場所以外を、すでにブロックで埋めたフィールドを作成
    Field freeze = wallField.freeze(calculator.getHeight());
    Field invertedOuterField = calculator.parseInvertedOuterField(outerColumnField);
    freeze.merge(invertedOuterField);
    // 置くブロック以外がすでに埋まっていると仮定したとき、正しく接着できる順があるか確認
    SeparableMinos separableMinos = calculator.getSeparableMinos();
    RecursiveMinoField result = new RecursiveMinoField(currentMino, outerColumnField.freeze(calculator.getHeight()), separableMinos);
    // 次に埋めるべき場所がないときは結果をそのまま返す
    if (needFilledField.isPerfect()) {
        return Stream.of(result);
    }
    // 現在のフィールドに埋めるべきところがないが、次以降のフィールドに残っている場合をケアする
    List<SeparableMino> allMinos = separableMinos.getMinos();
    return over(columnField, outerColumnField, allMinos, separableMinos, result);
}
Also used : ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Field(core.field.Field) SeparableMinos(searcher.pack.SeparableMinos) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField)

Example 4 with RecursiveMinoField

use of searcher.pack.mino_field.RecursiveMinoField in project solution-finder by knewjade.

the class MinoFieldComparatorTest method compareMinoFieldDiffOuterField.

@Test
void compareMinoFieldDiffOuterField() {
    Randoms randoms = new Randoms();
    List<SeparableMino> minos = separableMinos.getMinos();
    int index = randoms.nextInt(0, minos.size() - 1);
    SeparableMino mino = minos.get(index);
    RecursiveMinoField recursiveMinoField1 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    RecursiveMinoField recursiveMinoField2 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(1L), separableMinos);
    MinoFieldComparator comparator = new MinoFieldComparator();
    assertThat(comparator.compare(recursiveMinoField1, recursiveMinoField2)).isEqualTo(0);
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Test(org.junit.jupiter.api.Test)

Example 5 with RecursiveMinoField

use of searcher.pack.mino_field.RecursiveMinoField in project solution-finder by knewjade.

the class MinoFieldComparatorTest method compareMinoFieldEqual.

@Test
void compareMinoFieldEqual() {
    Randoms randoms = new Randoms();
    List<SeparableMino> minos = separableMinos.getMinos();
    SeparableMino mino = minos.get(0);
    RecursiveMinoField recursiveMinoField1 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    RecursiveMinoField recursiveMinoField2 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    for (int count = 0; count < 10; count++) {
        int index = randoms.nextInt(0, minos.size() - 1);
        SeparableMino randomMino = minos.get(index);
        recursiveMinoField1 = new RecursiveMinoField(randomMino, recursiveMinoField1, ColumnFieldFactory.createField(), separableMinos);
        recursiveMinoField2 = new RecursiveMinoField(randomMino, recursiveMinoField2, ColumnFieldFactory.createField(), separableMinos);
    }
    MinoFieldComparator comparator = new MinoFieldComparator();
    assertThat(comparator.compare(recursiveMinoField1, recursiveMinoField2)).isEqualTo(0);
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Test(org.junit.jupiter.api.Test)

Aggregations

RecursiveMinoField (searcher.pack.mino_field.RecursiveMinoField)7 SeparableMino (searcher.pack.separable_mino.SeparableMino)7 Randoms (lib.Randoms)4 Test (org.junit.jupiter.api.Test)4 ColumnField (core.column_field.ColumnField)3 Field (core.field.Field)3 SeparableMinos (searcher.pack.SeparableMinos)3 OperationWithKey (common.datastore.OperationWithKey)2 List (java.util.List)2 Objects (java.util.Objects)2 Callable (java.util.concurrent.Callable)2 Stream (java.util.stream.Stream)2 ColumnFieldConnection (searcher.pack.connections.ColumnFieldConnection)2 ColumnFieldConnections (searcher.pack.connections.ColumnFieldConnections)2 StreamColumnFieldConnections (searcher.pack.connections.StreamColumnFieldConnections)2 RecursiveMinoFields (searcher.pack.mino_fields.RecursiveMinoFields)2