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;
}
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));
}
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;
}
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;
}
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;
}
Aggregations