Ejemplo n.º 1
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                long pepper = -1;
                List <(long, string)> keys = new List <(long, string)>();
                Dictionary <char, List <(long, string)> > candidates = new Dictionary <char, List <(long, string)> >();
                while (keys.Count < 64)
                {
                    // Increment our ounter
                    pepper++;

                    // Clean invalid potential keys
                    CleanCandidates(candidates, pepper - 1000);

                    // Compute the hash
                    string hash = Md5.ComputeString(_salt, pepper);

                    // Check if we have quintuples
                    foreach (var kvp in candidates)
                    {
                        if (kvp.Value.Any() && Repetition.Continuous(hash, kvp.Key, 5))
                        {
                            // Validate those keys
                            keys.AddRange(kvp.Value);

                            // Check if we have more than 64 keys
                            if (keys.Count >= 64)
                            {
                                return(keys[63].Item1.ToString());
                            }

                            // Clear candidates
                            kvp.Value.Clear();
                        }
                    }

                    // Check if we have a triplicates
                    char?firstTriple = Repetition.First(hash, 3);
                    if (firstTriple.HasValue)
                    {
                        if (!candidates.ContainsKey(firstTriple.Value))
                        {
                            candidates.Add(firstTriple.Value, new List <(long, string)>());
                        }
                        candidates[firstTriple.Value].Add((pepper, hash));
                    }
                }
                return(pepper.ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                long pepper = -1;
                List <(long, string)> keys = new List <(long, string)>();
                Dictionary <char, List <(long, string)> > candidates = new Dictionary <char, List <(long, string)> >();
                while (keys.Count < 64)
                {
                    // Increment our ounter
                    pepper++;

                    // Clean invalid potential keys
                    CleanCandidates(candidates, pepper - 1000);

                    // Compute the hash
                    byte[] hash = Md5.Compute(_salt, pepper);
                    for (int i = 0; i < 2015; ++i)
                    {
                        hash = Md5.Compute(hash);
                    }

                    // Check if we have quintuples
                    string hashString = Md5.ComputeString(hash);
                    foreach (var kvp in candidates)
                    {
                        if (kvp.Value.Any() && Repetition.Continuous(hashString, kvp.Key, 5))
                        {
                            // Validate those keys
                            keys.AddRange(kvp.Value);

                            // Check if we have more than 64 keys
                            if (keys.Count >= 64)
                            {
                                return(keys[63].Item1.ToString());
                            }

                            // Clear candidates
                            kvp.Value.Clear();
                        }
                    }

                    // Check if we have a triplicates
                    char?firstTriple = Repetition.First(hashString, 3);
                    if (firstTriple.HasValue)
                    {
                        if (!candidates.ContainsKey(firstTriple.Value))
                        {
                            candidates.Add(firstTriple.Value, new List <(long, string)>());
                        }
                        candidates[firstTriple.Value].Add((pepper, hashString));
                    }
                }

                return(pepper.ToString());
            }

            return("");
        }