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); }
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; } } } }
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())); }
public void Part2() { string answer = KnotHash.HashString(_input); Assert.Equal("541dc3180fd4b72881e39cf925a50253", answer); }