示例#1
0
        public void Part1()
        {
            int[] lengths = _input.Split(',').Select(int.Parse).ToArray();

            KnotHash hash = new KnotHash(256);

            foreach (int length in lengths)
            {
                hash.Apply(length);
            }

            int answer = hash.Enumerate().Take(2).Aggregate((x, y) => x * y);

            Assert.Equal(23715, answer);
        }
示例#2
0
        public static string HashString(string input, int size = 256)
        {
            int[]    lengths = input.Select(c => (int)c).Concat(new[] { 17, 31, 73, 47, 23 }).ToArray();
            KnotHash hash    = new KnotHash(size);

            for (int i = 0; i < 64; i++)
            {
                foreach (int length in lengths)
                {
                    hash.Apply(length);
                }
            }

            return(hash.Enumerate()
                   .Chunk(16)
                   .Select(chunk => chunk.Aggregate((x, y) => x ^ y))
                   .Select(n => string.Format("{0:x2}", n))
                   .Aggregate(new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString()));
        }