Пример #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("");
        }
Пример #2
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                long     count     = 0;
                string[] forbidden = new string[] { "ab", "cd", "pq", "xy" };
                foreach (string s in _input)
                {
                    int voyels = s.ToCharArray().Count(c => "aeiou".Contains(c));
                    if (voyels < 3)
                    {
                        continue;
                    }

                    char?repeated = Repetition.First(s, 2);
                    if (repeated == null)
                    {
                        continue;
                    }

                    bool wrong = false;
                    foreach (string f in forbidden)
                    {
                        if (s.Contains(f))
                        {
                            wrong = true;
                            break;
                        }
                    }
                    if (wrong)
                    {
                        continue;
                    }

                    count++;
                }

                return(count.ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                long count = 0;
                foreach (string s in _input)
                {
                    if (!Repetition.Group(s, 2))
                    {
                        continue;
                    }

                    if (!Repetition.Spaced(s, 1))
                    {
                        continue;
                    }

                    count++;
                }

                return(count.ToString());
            }

            return("");
        }