Search in sources :

Example 1 with AsyncBufferedFileWriter

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);
}
Also used : Piece(core.mino.Piece) Pieces(common.datastore.blocks.Pieces) PatternGenerator(common.pattern.PatternGenerator) Set(java.util.Set) ReducePatternGenerator(entry.path.ReducePatternGenerator) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) List(java.util.List) Field(core.field.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) Comparator(java.util.Comparator) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) Set(java.util.Set) HashSet(java.util.HashSet) PieceCounter(common.datastore.PieceCounter) IOException(java.io.IOException) PathPair(entry.path.PathPair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) Piece(core.mino.Piece) List(java.util.List) FinderExecuteException(exceptions.FinderExecuteException)

Example 2 with AsyncBufferedFileWriter

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);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) Piece(core.mino.Piece) java.util(java.util) LongPieces(common.datastore.blocks.LongPieces) PatternGenerator(common.pattern.PatternGenerator) ReducePatternGenerator(entry.path.ReducePatternGenerator) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) Field(core.field.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) OperationWithKey(common.datastore.OperationWithKey) PieceCounter(common.datastore.PieceCounter) IOException(java.io.IOException) PathPair(entry.path.PathPair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) Piece(core.mino.Piece) FinderExecuteException(exceptions.FinderExecuteException)

Example 3 with AsyncBufferedFileWriter

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);
}
Also used : Piece(core.mino.Piece) LongPieces(common.datastore.blocks.LongPieces) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) SizedBit(searcher.pack.SizedBit) List(java.util.List) Field(core.field.Field) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) Piece(core.mino.Piece) List(java.util.List) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) HashSet(java.util.HashSet)

Aggregations

Field (core.field.Field)3 Piece (core.mino.Piece)3 PathEntryPoint (entry.path.PathEntryPoint)3 PathPair (entry.path.PathPair)3 PathSettings (entry.path.PathSettings)3 FinderExecuteException (exceptions.FinderExecuteException)3 FinderInitializeException (exceptions.FinderInitializeException)3 File (java.io.File)3 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 AsyncBufferedFileWriter (lib.AsyncBufferedFileWriter)3 SizedBit (searcher.pack.SizedBit)3 PieceCounter (common.datastore.PieceCounter)2 LongPieces (common.datastore.blocks.LongPieces)2 PatternGenerator (common.pattern.PatternGenerator)2 ReducePatternGenerator (entry.path.ReducePatternGenerator)2 HashSet (java.util.HashSet)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OperationWithKey (common.datastore.OperationWithKey)1