Exemple #1
0
        static void Main(string[] args)
        {
            var input  = "ffayrhll";
            var hasher = new KnotHasher();
            var lines  = new String[FieldSize];

            for (int row = 0; row < FieldSize; row++)
            {
                var hashInput = $"{input}-{row}";
                var hash      = hasher.Hash(hashInput);

                var bitstring = String.Empty;
                foreach (var c in hash)
                {
                    bitstring += HexvalueToBits(c.ToString());
                }

                lines[row] = bitstring;
            }

            var filled = lines.SelectMany(l => l).Count(c => c == '1');

            Console.WriteLine(filled);

            var groupMap   = new Int32[FieldSize, FieldSize];
            var groupCount = 0;

            for (int row = 0; row < FieldSize; row++)
            {
                for (int column = 0; column < FieldSize; column++)
                {
                    if (NeedsToBeGrouped(new Point(column, row), lines, groupMap))
                    {
                        FormGroup(++groupCount, new Point(column, row), lines, groupMap);
                    }
                }
            }

            Console.WriteLine(groupCount);
            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            int[,] value = new int[128, 128];
            for (int i = 0; i < 128; i++)
            {
                string hash   = KnotHasher.ComputeKnotHash($"{input}-{i}");
                int[]  toBits = ConvertToBits(hash);

                for (int j = 0; j < toBits.Length; j++)
                {
                    value[j, i] = toBits[j];
                }
            }

            int             groupCounter = 0;
            HashSet <Point> usedPoints   = new HashSet <Point>();

            for (int y = 0; y < 128; y++)
            {
                for (int x = 0; x < 128; x++)
                {
                    Point item = new Point(x, y);
                    if (value[x, y] == 1 && !usedPoints.Contains(item))
                    {
                        groupCounter++;
                        usedPoints.Add(item);

                        Stack <Point> pointsToCheck = new Stack <Point>();
                        pointsToCheck.Push(item);

                        while (pointsToCheck.Any())
                        {
                            Point pt = pointsToCheck.Pop();

                            if (pt.X < 127)
                            {
                                Point ptn = new Point(pt.X + 1, pt.Y);
                                if (value[pt.X + 1, pt.Y] == 1 && !usedPoints.Contains(ptn))
                                {
                                    usedPoints.Add(ptn);
                                    pointsToCheck.Push(ptn);
                                }
                            }

                            if (pt.Y < 127)
                            {
                                Point ptn = new Point(pt.X, pt.Y + 1);

                                if (value[pt.X, pt.Y + 1] == 1 && !usedPoints.Contains(ptn))
                                {
                                    usedPoints.Add(ptn);
                                    pointsToCheck.Push(ptn);
                                }
                            }

                            if (pt.X > 0)
                            {
                                Point ptn = new Point(pt.X - 1, pt.Y);

                                if (value[pt.X - 1, pt.Y] == 1 && !usedPoints.Contains(ptn))
                                {
                                    usedPoints.Add(ptn);
                                    pointsToCheck.Push(ptn);
                                }
                            }

                            if (pt.Y > 0)
                            {
                                Point ptn = new Point(pt.X, pt.Y - 1);

                                if (value[pt.X, pt.Y - 1] == 1 && !usedPoints.Contains(ptn))
                                {
                                    usedPoints.Add(ptn);
                                    pointsToCheck.Push(ptn);
                                }
                            }
                        }
                    }
                }
            }


            Console.WriteLine($"There are {groupCounter} groups");
            Console.ReadKey(true);
        }