示例#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 Day14()
        {
            _map = new bool[128, 128];

            for (int i = 0; i < 128; i++)
            {
                string hash = KnotHash.HashString($"{Seed}-{i}");

                int col = 0;
                foreach (char c in hash)
                {
                    int num = c <= '9' ? c - '0' : c - 'a' + 10;
                    for (int bit = 3; bit >= 0; bit--)
                    {
                        _map[i, col++] = (num & (1 << bit)) != 0;
                    }
                }
            }
        }
示例#3
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()));
        }
示例#4
0
        public void Part2()
        {
            string answer = KnotHash.HashString(_input);

            Assert.Equal("541dc3180fd4b72881e39cf925a50253", answer);
        }