Exemple #1
0
        private static int FindPuzzleWithConstraint()
        {
            var lockObj = new object();

            Db.ConnectionString = @"Server=CORNFLOWER;Database=Kyudosudoku;Trusted_Connection=True;";
            var notFound = new HashSet <string> {
                "YSum"
            };

            Enumerable.Range(192, 1000).ParallelForEach(Environment.ProcessorCount, seed =>
            {
                using (var db = new Db())
                    if (db.Puzzles.Any(p => p.PuzzleID == seed))
                    {
                        return;
                    }
                lock (lockObj)
                {
                    if (notFound.Count == 0)
                    {
                        return;
                    }
                    Console.WriteLine(seed);
                }
                var puz = Kyudosudoku.Generate(seed);
                lock (lockObj)
                {
                    if (notFound.Count == 0)
                    {
                        return;
                    }
                    foreach (var lk in puz.Constraints)
                    {
                        var str = lk.GetType().Name;
                        if (notFound.Contains(str))
                        {
                            puz.SaveToDb(seed, null);
                            ConsoleUtil.WriteLine($" — {seed} has {str}".Color(ConsoleColor.White, ConsoleColor.DarkGreen));
                            notFound.Remove(str);
                        }
                    }
                }
            });

            Console.WriteLine("Done.");
            Console.ReadLine();
            return(0);
        }
Exemple #2
0
        private static void RunStatistics()
        {
            var lockObj              = new object();
            var seedCounter          = 1000;
            var stats                = new Dictionary <string, int>();
            var arrowLengthStats     = new Dictionary <int, int>();
            var inclusionNumStats    = new Dictionary <int, int>();
            var killerCageSizeStats  = new Dictionary <int, int>();
            var killerCageSumStats   = new Dictionary <int, int>();
            var renbanCageSizeStats  = new Dictionary <int, int>();
            var palindromeSizeStats  = new Dictionary <int, int>();
            var thermometerSizeStats = new Dictionary <int, int>();
            var cappedLineSizeStats  = new Dictionary <int, int>();

            //var json = JsonValue.Parse(File.ReadAllText(@"D:\temp\kyudo-stats.json"));
            //var strings = @"AntiBishop,AntiKing,AntiKnight,NoConsecutive,OddEven,,,Arrow,KillerCage,Palindrome,RenbanCage,Snowball,Thermometer,,,Battlefield,Binairo,Sandwich,Skyscraper,ToroidalSandwich,XSum,,,Battenburg,Clockface,ConsecutiveNeighbors,DoubleNeighbors,Inclusion,LittleKiller".Split(',');
            //Clipboard.SetText(strings.Select(s => string.IsNullOrWhiteSpace(s) ? "" : json["Stats"][s].GetInt().ToString()).JoinString("\n"));
            //return;

            Enumerable.Range(0, Environment.ProcessorCount).ParallelForEach(proc =>
            {
                var seed = 0;
                while (true)
                {
                    lock (lockObj)
                    {
                        seed = seedCounter++;
                        Console.WriteLine($"Generating {seed}");
                    }
                    var puzzle = Kyudosudoku.Generate(seed);
                    lock (lockObj)
                    {
                        foreach (var constr in puzzle.Constraints)
                        {
                            stats.IncSafe(constr.GetType().Name);
                            if (constr is Arrow a)
                            {
                                arrowLengthStats.IncSafe(a.Cells.Length - 1);
                            }
                            if (constr is Inclusion i)
                            {
                                inclusionNumStats.IncSafe(i.Digits.Length);
                            }
                            if (constr is KillerCage kc)
                            {
                                killerCageSizeStats.IncSafe(kc.Cells.Length); killerCageSumStats.IncSafe(kc.Sum ?? -1);
                            }
                            if (constr is RenbanCage rc)
                            {
                                renbanCageSizeStats.IncSafe(rc.Cells.Length);
                            }
                            if (constr is Palindrome p)
                            {
                                palindromeSizeStats.IncSafe(p.Cells.Length);
                            }
                            if (constr is Thermometer t)
                            {
                                thermometerSizeStats.IncSafe(t.Cells.Length);
                            }
                            if (constr is CappedLine cl)
                            {
                                cappedLineSizeStats.IncSafe(cl.Cells.Length);
                            }
                        }
                        ClassifyJson.SerializeToFile(new
                        {
                            Stats               = stats,
                            ArrowLengths        = arrowLengthStats,
                            InclusionNums       = inclusionNumStats,
                            KillerCageSizes     = killerCageSizeStats,
                            KillerCageSums      = killerCageSumStats,
                            RenbanCageSizes     = renbanCageSizeStats,
                            PalindromeSizes     = palindromeSizeStats,
                            ThermometerSizes    = thermometerSizeStats,
                            CappedLineSizeStats = cappedLineSizeStats
                        }, @"D:\temp\kyudo-stats.json");
                    }
                }
            });
        }