Exemplo n.º 1
0
        public string PartA()
        {
            List <int>    elements      = Enumerable.Range(0, 256).ToList();
            KnotHashState previousState = new KnotHashState(elements, 0, 0);

            KnotHash      knotHash = new KnotHash();
            KnotHashState newState = knotHash.CalculateKnotHash(previousState, Input);
            int           mult     = newState.Elements[0] * newState.Elements[1];

            return(mult.ToString());
        }
Exemplo n.º 2
0
        public KnotHashState CalculateKnotHash(KnotHashState previousState, List <int> inputs)
        {
            List <int> elements        = previousState.Elements;
            int        skipSize        = previousState.SkipSize;
            int        currentPosition = previousState.CurrentPosition;

            for (int length = 0; length < inputs.Count; length++)
            {
                int        reverseTo      = inputs[length];
                int        reversePointer = 0;
                List <int> reversed       = new List <int>();
                for (int x = 0; x < reverseTo; x++)
                {
                    int localPointer = currentPosition + x;
                    if (localPointer >= elements.Count)
                    {
                        localPointer = localPointer - elements.Count;
                    }

                    reversed.Add(elements[localPointer]);
                }

                reversed.Reverse();
                for (int y = 0; y < reverseTo; y++)
                {
                    int localPointer = currentPosition + y;
                    if (localPointer >= elements.Count)
                    {
                        localPointer = localPointer - elements.Count;
                    }

                    elements[localPointer] = reversed[reversePointer];
                    reversePointer++;
                }

                // Move the currentPosition forward by length
                currentPosition += (inputs[length] + skipSize);
                if (currentPosition >= elements.Count)
                {
                    currentPosition = currentPosition % elements.Count;
                }

                skipSize++;
            }

            return(new KnotHashState(elements, currentPosition, skipSize));
        }
Exemplo n.º 3
0
        private string CommonHexConstructor(List <int> asciiCodes)
        {
            List <int> extraCodes = new List <int>()
            {
                17, 31, 73, 47, 23
            };
            List <int> merged = asciiCodes.Concat(extraCodes).ToList();

            List <int>    elements      = Enumerable.Range(0, 256).ToList();
            KnotHashState previousState = new KnotHashState(elements, 0, 0);

            int counter = 0;

            while (counter < 64)
            {
                previousState = CalculateKnotHash(previousState, merged);
                counter++;
            }

            List <int> denseHash = ToDenseHash(previousState.Elements, 256);
            string     hex       = ToHexString(denseHash);

            return(hex);
        }