Ejemplo n.º 1
0
        public static int First(string input)
        {
            // lookup table for number of set bits for each hex digit
            Dictionary <char, int> hexBits = new Dictionary <char, int>
            {
                ['0'] = 0,
                ['1'] = 1,
                ['2'] = 1,
                ['3'] = 2,
                ['4'] = 1,
                ['5'] = 2,
                ['6'] = 2,
                ['7'] = 3,
                ['8'] = 1,
                ['9'] = 2,
                ['a'] = 2,
                ['b'] = 3,
                ['c'] = 2,
                ['d'] = 3,
                ['e'] = 3,
                ['f'] = 4,
            };

            var used = 0;

            // over all hashes...
            for (int i = 0; i < 128; i++)
            {
                var hash = Day10.Second($"{input}-{i}");
                // sum the number of set bits.
                foreach (var c in hash)
                {
                    used += hexBits[c];
                }
            }
            return(used);
        }
Ejemplo n.º 2
0
        public static int Second(string input)
        {
            Dictionary <char, string> hexCharBinString = new Dictionary <char, string> {
                ['0'] = "0000",
                ['1'] = "0001",
                ['2'] = "0010",
                ['3'] = "0011",
                ['4'] = "0100",
                ['5'] = "0101",
                ['6'] = "0110",
                ['7'] = "0111",
                ['8'] = "1000",
                ['9'] = "1001",
                ['a'] = "1010",
                ['b'] = "1011",
                ['c'] = "1100",
                ['d'] = "1101",
                ['e'] = "1110",
                ['f'] = "1111",
            };

            bool[,] lines = new bool[128, 128];
            for (int y = 0; y < 128; y++)
            {
                var hash = Day10.Second($"{input}-{y}");
                var bits = string.Join("", hash.Select(c => hexCharBinString[c]));
                for (int x = 0; x < 128; x++)
                {
                    lines[x, y] = bits[x] == '1';
                }
            }

            var graphInput = "";

            for (int y = 0; y < 128; y++)
            {
                for (int x = 0; x < 128; x++)
                {
                    if (lines[x, y])
                    {
                        var adjacents = new List <int>();
                        if (y > 0 && lines[x, y - 1])
                        {
                            adjacents.Add(x + (y - 1) * 128);
                        }
                        if (y < 127 && lines[x, y + 1])
                        {
                            adjacents.Add(x + (y + 1) * 128);
                        }
                        if (x > 0 && lines[x - 1, y])
                        {
                            adjacents.Add((x - 1) + y * 128);
                        }
                        if (x < 127 && lines[x + 1, y])
                        {
                            adjacents.Add((x + 1) + y * 128);
                        }

                        if (adjacents.Any())
                        {
                            graphInput += (x + y * 128) + " <-> " + string.Join(", ", adjacents) + '\n';
                        }
                    }
                }
            }

            return(ConnectedGroups(graphInput, lines));
        }