Exemplo n.º 1
0
        public static void SolvePuzzle8_P2(string inputFile)
        {
            List <InstructionP2> instructionSet = new List <InstructionP2>();

            using (StreamReader sr = new StreamReader(inputFile))
            {
                string buffer = sr.ReadLine();
                while (buffer != null)
                {
                    var instruct = buffer.Split(' ');
                    instructionSet.Add(new InstructionP2(instruct[0], instruct[1]));
                    buffer = sr.ReadLine();
                }
            }

            long          accumulator     = 0;
            int           curIndex        = 0;
            HashSet <int> visitedIndexes  = new HashSet <int>();
            List <int>    suspectIndicies = new List <int>();

            while (true)
            {
                if (visitedIndexes.Contains(curIndex))
                {
                    Console.WriteLine($"Reached operation to be executed a second time. Suspect list contains: {suspectIndicies.Count} items. ");
                    break;
                }

                visitedIndexes.Add(curIndex);
                InstructionP2 CI = instructionSet[curIndex]; // just for readability.
                switch (CI.Operation)
                {
                case "acc":
                    accumulator += CI.OpValue;
                    curIndex++;
                    break;

                case "nop":     // OpValue will always be 0
                    suspectIndicies.Add(curIndex);
                    curIndex++;
                    break;

                case "jmp":
                    suspectIndicies.Add(curIndex);
                    curIndex += CI.OpValue;
                    break;
                }
            }

            foreach (int sus in suspectIndicies)
            {
                accumulator = IterateWithSuspectIndex(instructionSet, sus);
                if (accumulator != int.MinValue)
                {
                    Console.WriteLine($"Completed iteration after changing index {sus}, accumulator = {accumulator}");
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private static long IterateWithSuspectIndex(List <InstructionP2> instructionSet, int suspectIndex)
        {
            long          accumulator    = 0;
            int           curIndex       = 0;
            HashSet <int> visitedIndexes = new HashSet <int>();

            while (true)
            {
                if (visitedIndexes.Contains(curIndex))
                {
                    Console.WriteLine($"Reached operation to be executed a second time. Suspect {suspectIndex} is not the culpruit.");
                    return(int.MinValue);
                }

                visitedIndexes.Add(curIndex);
                InstructionP2 CI = instructionSet[curIndex]; // just for readability.
                switch (CI.Operation)
                {
                case "acc":
                    accumulator += CI.OpValue;
                    curIndex++;
                    break;

                case "nop":     // OpValue will always be 0
                    if (curIndex == suspectIndex)
                    {
                        curIndex += CI.OpValue;
                    }
                    else
                    {
                        curIndex++;
                    }
                    break;

                case "jmp":
                    if (curIndex == suspectIndex)
                    {
                        curIndex++;
                    }
                    else
                    {
                        curIndex += CI.OpValue;
                    }
                    break;
                }
                if (curIndex == instructionSet.Count)
                {
                    return(accumulator);
                }
            }
        }