Пример #1
0
        private void ProcessInstruction(InstructionModel instruction)
        {
            if (instruction.Completed)
            {
                return;
            }

            InstructionModel nextInstruction;

            switch (instruction.Instruction)
            {
            case "acc":
                _answer        += instruction.Direction == "+" ? instruction.Value : -instruction.Value;
                nextInstruction = _data.First(a => a.Index == instruction.Index + 1);
                break;

            case "jmp":
                var nextIndex = instruction.Index + (instruction.Direction == "+" ? instruction.Value : -instruction.Value);
                nextInstruction = _data.First(a => a.Index == nextIndex);
                break;

            case "nop":
                nextInstruction = _data.First(a => a.Index == instruction.Index + 1);
                break;

            default:
                throw new InvalidDataException("Invalid data at index " + instruction.Index);
            }

            instruction.Completed = true;
            ProcessInstruction(nextInstruction);
        }
Пример #2
0
 private void SwitchInstruction(InstructionModel instruction)
 {
     if (instruction.Instruction == "jmp")
     {
         instruction.Instruction = "nop";
     }
     else if (instruction.Instruction == "nop")
     {
         instruction.Instruction = "jmp";
     }
 }
Пример #3
0
        public static List <InstructionModel> GetData()
        {
            var result = new List <InstructionModel>();

            using (var sr = new StreamReader("./DayEight/input.txt"))
            {
                string line;
                var    index = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    var instruction = new InstructionModel();
                    instruction.Index       = index;
                    instruction.Instruction = line.Substring(0, 3);
                    instruction.Direction   = line.Substring(4, 1);
                    instruction.Value       = int.Parse(line.Substring(5));
                    index++;
                    result.Add(instruction);
                }
            }

            return(result);
        }