Example #1
0
        public override string Part2()
        {
            var testChars = PuzzleInput.ToUpper().Distinct().ToHashSet();

            var polymerLength = PuzzleInput.Count();

            Parallel.ForEach(testChars, (testChar) =>
            {
                var shifts = PuzzleInput.ToList();

                //Remove 1 problem causing polymer
                shifts.RemoveAll(c => c == testChar || c == (char)(testChar + 32));
                bool changed;
                do
                {
                    changed = false;
                    for (int i = 0; i < shifts.Count() - 1; i++)
                    {
                        if ((shifts[i] - shifts[i + 1]) % 32 == 0 && (shifts[i] != shifts[i + 1]))
                        {
                            shifts.RemoveAt(i);
                            shifts.RemoveAt(i);
                            changed = true;
                        }
                    }
                } while (changed);
                if (shifts.Count() < polymerLength)
                {
                    lock (varSafe) polymerLength = shifts.Count();
                }
            });
            return(polymerLength.ToString());
        }
Example #2
0
        public override string Part1()
        {
            var  shifts = PuzzleInput.ToList();
            bool changed;
            var  prevCount = shifts.Count();

            do
            {
                changed = false;
                for (int i = 0; i < shifts.Count() - 1; i++)
                {
                    if (((int)shifts[i] - (int)shifts[i + 1]) % 32 == 0 && (shifts[i] != shifts[i + 1]))
                    {
                        shifts.RemoveAt(i);
                        shifts.RemoveAt(i);
                        changed = true;
                    }
                }
            } while (changed);
            return(shifts.Count().ToString());
        }