use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class OrderLookupTest method forwardJustBlocksRandom.
@Test
void forwardJustBlocksRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 13; size++) {
List<Piece> pieceList = randoms.blocks(size);
int toDepth = pieceList.size();
PiecesNumberComparator comparator = new PiecesNumberComparator();
List<LongPieces> forward1 = OrderLookup.forwardBlocks(pieceList, toDepth).stream().map(StackOrder::toList).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
ForwardOrderLookUp lookUp = new ForwardOrderLookUp(toDepth, pieceList.size());
List<LongPieces> forward2 = lookUp.parse(pieceList).map(blockStream -> blockStream.collect(Collectors.toList())).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
assertThat(forward2).isEqualTo(forward1);
}
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class FumenCSVPathOutput method output.
@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
this.lastException = null;
outputLog("Found path = " + pathPairs.size());
try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
writer.writeAndNewLine("テト譜,使用ミノ,対応ツモ数 (対地形),対応ツモ数 (対パターン),ツモ (対地形),ツモ (対パターン)");
pathPairs.parallelStream().map(pathPair -> {
// テト譜
String encode = pathPair.getFumen();
// パターンに対する有効なミノ順をまとめる
List<LongPieces> patternBuildBlocks = pathPair.blocksStreamForPattern().collect(Collectors.toList());
// 対応ツモ数 (対パターン)
int pattern = patternBuildBlocks.size();
// 使用ミノをまとめる
String usingPieces = pathPair.getUsingBlockName();
// パターンに対する有効なミノ順をまとめる
String validOrdersPattern = patternBuildBlocks.stream().map(longBlocks -> longBlocks.blockStream().map(Piece::getName).collect(Collectors.joining())).collect(Collectors.joining(";"));
// 地形に対する有効なミノ順をまとめる
HashSet<LongPieces> solutionBuildBlocks = pathPair.blocksHashSetForSolution();
String validOrdersSolution = solutionBuildBlocks.stream().map(longBlocks -> longBlocks.blockStream().map(Piece::getName).collect(Collectors.joining())).collect(Collectors.joining(";"));
// 対応ツモ数 (対地形)
int solution = solutionBuildBlocks.size();
return String.format("http://fumen.zui.jp/?v115@%s,%s,%d,%d,%s,%s", encode, usingPieces, solution, pattern, validOrdersSolution, validOrdersPattern);
}).forEach(writer::writeAndNewLine);
writer.flush();
} catch (IOException e) {
throw new FinderExecuteException("Failed to output file", e);
}
if (lastException != null)
throw new FinderExecuteException("Error to output file", lastException);
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PathCore method run.
List<PathPair> run(Field field, SizedBit sizedBit) throws ExecutionException, InterruptedException {
List<Result> candidates = searcher.toList();
int maxClearLine = sizedBit.getHeight();
return candidates.parallelStream().map(result -> {
LinkedList<MinoOperationWithKey> operations = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
// 地形の中で組むことができるoperationsをすべてリスト化する
BuildUpStream buildUpStream2 = buildUpStreamThreadLocal.get();
List<List<MinoOperationWithKey>> validOperaions = buildUpStream2.existsValidBuildPatternDirectly(field, operations).collect(Collectors.toList());
// 地形の中で組むことができるものがないときはスキップ
if (validOperaions.isEmpty())
return PathPair.EMPTY_PAIR;
// 地形の中で組むことができるSetを作成
HashSet<LongPieces> piecesSolution = validOperaions.stream().map(operationWithKeys -> operationWithKeys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
// 探索シーケンスの中で組むことができるSetを作成
HashSet<LongPieces> piecesPattern = getPiecesPattern(piecesSolution);
// 探索シーケンスの中で組むことができるものがないときはスキップ
if (piecesPattern.isEmpty())
return PathPair.EMPTY_PAIR;
// 探索シーケンスの中でテト譜にするoperationsを選択する
List<MinoOperationWithKey> operationsToUrl = validOperaions.stream().filter(o -> validPieces.contains(new LongPieces(o.stream().map(Operation::getPiece)))).findFirst().orElse(Collections.emptyList());
// 譜面の作成
String fumen = fumenParser.parse(operationsToUrl, field, maxClearLine);
return new PathPair(result, piecesSolution, piecesPattern, fumen, new ArrayList<>(operationsToUrl), validPieces);
}).filter(pathPair -> pathPair != PathPair.EMPTY_PAIR).collect(Collectors.toList());
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PathCore method getPiecesPattern.
private HashSet<LongPieces> getPiecesPattern(HashSet<LongPieces> piecesSolution) {
if (isHoldReduced) {
// allとvalidが異なる
ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(maxDepth, maxDepth + 1);
return piecesSolution.stream().filter(validPieces::contains).flatMap(blocks -> {
return reverseOrderLookUp.parse(blocks.getPieces()).map(stream -> stream.collect(Collectors.toCollection(ArrayList::new))).flatMap(blocksWithHold -> {
int nullIndex = blocksWithHold.indexOf(null);
if (nullIndex < 0)
return Stream.of(new LongPieces(blocksWithHold));
Stream.Builder<LongPieces> builder = Stream.builder();
for (Piece piece : Piece.values()) {
blocksWithHold.set(nullIndex, piece);
builder.accept(new LongPieces(blocksWithHold));
}
return builder.build();
});
}).filter(allPieces::contains).collect(Collectors.toCollection(HashSet::new));
} else if (isUsingHold) {
// allとvalidが同じだが、ホールドが使える
ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(maxDepth, maxDepth);
return piecesSolution.stream().filter(validPieces::contains).flatMap(blocks -> {
return reverseOrderLookUp.parse(blocks.getPieces()).map(stream -> stream.collect(Collectors.toCollection(ArrayList::new))).flatMap(blocksWithHold -> {
int nullIndex = blocksWithHold.indexOf(null);
if (nullIndex < 0)
return Stream.of(new LongPieces(blocksWithHold));
Stream.Builder<LongPieces> builder = Stream.builder();
for (Piece piece : Piece.values()) {
blocksWithHold.set(nullIndex, piece);
builder.accept(new LongPieces(blocksWithHold));
}
return builder.build();
});
}).filter(allPieces::contains).collect(Collectors.toCollection(HashSet::new));
} else {
// そのまま絞り込みだけ実施
return piecesSolution.stream().filter(validPieces::contains).collect(Collectors.toCollection(HashSet::new));
}
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PermutationIterableTest method iteratorRandomBlock.
@Test
@LongTest
void iteratorRandomBlock() throws Exception {
Randoms randoms = new Randoms();
ArrayList<Piece> allPieces = Lists.newArrayList(Iterables.concat(Piece.valueList(), Piece.valueList()));
for (int pop = 1; pop <= 7; pop++) {
PermutationIterable<Piece> iterable = new PermutationIterable<>(allPieces, pop);
HashSet<LongPieces> sets = StreamSupport.stream(iterable.spliterator(), false).map(Collection::stream).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
// ランダムにサンプルを選択し、必ず列挙したセットの中にあることを確認
for (int count = 0; count < 1000; count++) {
List<Piece> sample = randoms.sample(allPieces, pop);
LongPieces pieces = new LongPieces(sample);
assertThat(pieces).isIn(sets);
}
}
}
Aggregations