use of lib.AsyncBufferedFileWriter in project solution-finder by knewjade.
the class PatternCSVPathOutput method output.
@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
this.lastException = null;
outputLog("Found path = " + pathPairs.size());
AtomicInteger validCounter = new AtomicInteger();
AtomicInteger allCounter = new AtomicInteger();
try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
writer.writeAndNewLine("ツモ,対応地形数,使用ミノ,未使用ミノ,テト譜");
generator.blocksStream().parallel().map(blocks -> {
// シーケンス名を取得
String sequenceName = blocks.blockStream().map(Piece::getName).collect(Collectors.joining());
// パフェ可能な地形を抽出
List<PathPair> valid = pathPairs.stream().filter(pathPair -> {
HashSet<? extends Pieces> buildBlocks = pathPair.blocksHashSetForPattern();
return buildBlocks.contains(blocks);
}).collect(Collectors.toList());
// パフェ可能な地形数
int possibleSize = valid.size();
// パフェ可能ならカウンターをインクリメント
allCounter.incrementAndGet();
if (0 < possibleSize)
validCounter.incrementAndGet();
// パフェ可能な地形のテト譜を連結
String fumens = valid.stream().sorted(Comparator.comparing(PathPair::getPatternSize).reversed()).map(pathPair -> "v115@" + pathPair.getFumen()).collect(Collectors.joining(";"));
// 使うミノ一覧を抽出
Set<PieceCounter> usesSet = valid.stream().map(PathPair::getBlockCounter).collect(Collectors.toSet());
String uses = usesSet.stream().map(blockCounter -> {
return blockCounter.getBlockStream().sorted().map(Piece::getName).collect(Collectors.joining());
}).collect(Collectors.joining(";"));
// 残せるミノ一覧を抽出
PieceCounter orderPieceCounter = new PieceCounter(blocks.blockStream());
String noUses = usesSet.stream().map(orderPieceCounter::removeAndReturnNew).distinct().map(blockCounter -> {
return blockCounter.getBlockStream().sorted().map(Piece::getName).collect(Collectors.joining());
}).collect(Collectors.joining(";"));
return String.format("%s,%d,%s,%s,%s", sequenceName, possibleSize, uses, noUses, fumens);
}).forEach(writer::writeAndNewLine);
writer.flush();
} catch (IOException e) {
throw new FinderExecuteException("Failed to output file", e);
}
outputLog("");
outputLog("perfect clear percent");
outputLog(String.format(" -> success = %.2f%% (%d/%d)", 100.0 * validCounter.get() / allCounter.get(), validCounter.get(), allCounter.get()));
if (lastException != null)
throw new FinderExecuteException("Error to output file", lastException);
}
use of lib.AsyncBufferedFileWriter in project solution-finder by knewjade.
the class UseCSVPathOutput method output.
@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
this.lastException = null;
outputLog("Found path = " + pathPairs.size());
AtomicInteger allCounter = new AtomicInteger();
Map<PieceCounter, List<PathPair>> groupingByClockCounter = pathPairs.parallelStream().collect(Collectors.groupingBy(pathPair -> {
List<OperationWithKey> operations = pathPair.getSampleOperations();
return new PieceCounter(operations.stream().map(OperationWithKey::getPiece));
}));
List<PathPair> emptyValidList = Collections.emptyList();
try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
writer.writeAndNewLine("使用ミノ,対応地形数,対応ツモ数 (対パターン),テト譜,ツモ (対パターン)");
generator.blockCountersStream().parallel().map(blockCounter -> {
// カウンターをインクリメント
allCounter.incrementAndGet();
// 組み合わせ名を取得
String blockCounterName = blockCounter.getBlockStream().map(Piece::getName).collect(Collectors.joining());
// パフェ可能な地形を抽出
List<PathPair> valid = groupingByClockCounter.getOrDefault(blockCounter, emptyValidList);
// パフェ可能な地形数
int possibleSize = valid.size();
// パフェ可能な地形のテト譜を連結
String fumens = valid.stream().sorted(Comparator.comparing(PathPair::getPatternSize).reversed()).map(pathPair -> "v115@" + pathPair.getFumen()).collect(Collectors.joining(";"));
// 対応できるパターンを重複なく抽出
Set<LongPieces> possiblePatternSet = valid.stream().flatMap(PathPair::blocksStreamForPattern).collect(Collectors.toSet());
// 対応できるパターン数
int possiblePatternSize = possiblePatternSet.size();
// パターンを連結
String patterns = possiblePatternSet.stream().map(LongPieces::getPieces).map(blocks -> blocks.stream().map(Piece::getName).collect(Collectors.joining(""))).collect(Collectors.joining(";"));
return String.format("%s,%d,%d,%s,%s", blockCounterName, possibleSize, possiblePatternSize, fumens, patterns);
}).forEach(writer::writeAndNewLine);
writer.flush();
} catch (IOException e) {
throw new FinderExecuteException("Failed to output file", e);
}
outputLog("Found piece combinations = " + allCounter.get());
if (lastException != null)
throw new FinderExecuteException("Error to output file", lastException);
}
use of lib.AsyncBufferedFileWriter 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);
}
Aggregations